Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS
Consider this recursive function:
Consider this recursive function:
def f(n):
if n <= 1:
return
f(n - 1)
if n % 2 == 0:
f(n - 2)How many times is f called in total, including the initial call, when f(9) is made?- A. 38
- B. 39
- C. 40
- D. 109
Correct answer: B. 39
Each call makes one call to f(n − 1) and a second call only when n is divisible by 2; counting every invocation gives 39. Forgetting the divisibility test gives 109, and counting only the calls that are not the initial one gives 38.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- A bipartite graph has left side P1, P2, P3, P4 and right side Q1, Q2, Q3, Q4, with edges P1–Q2, P1–Q3, P1–Q1, P2–Q3. What is the size of a…
- A tree has the vertices A, C, D, F, G, M, P, S, X, Y and the edges A–M, A–S, M–Y, A–F, M–D, D–X, Y–C, F–G, S–P. A set D dominates the tree…
- A grid has 5 rows and 7 columns (rows listed top to bottom; '.' is an open cell, '#' a wall): .#..... ....#.. .#####. .#..... ..#…
- A connected undirected graph has the vertices D, H, J, K, L, T and the edges J–L, J–D, J–H, D–K, D–T, K–T, T–H. How many bridges does it…
- A tree has the vertices D, E, G, H, I, T, U, X, Z and the edges D–T, D–H, T–G, H–E, E–X, D–Z, T–U, Z–I. What is the minimum number of…
- An undirected graph has the vertices E, J, K, M, X and the edges X–M, X–K, X–J, M–J, K–J, K–E. A walk may repeat vertices and edges. How…