Data Structures & AlgorithmsVery hardGATE CS · ICPC regional
Consider this recursive function:
Consider this recursive function:
def f(n):
if n <= 0:
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(10) is made?- A. 79
- B. 94
- C. 95
- D. 287
Correct answer: B. 94
Each call makes one call to f(n − 1) and a second call only when n is divisible by 2; counting every invocation gives 94. Forgetting the divisibility test gives 287, and counting only the calls that are not the initial one gives 93.
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
- Floyd–Warshall runs on vertices 1 to 4 with these arcs u→v (weight): 1→2 (4), 1→3 (8), 1→4 (4), 2→1 (5), 2→4 (2), 3→1 (6), 3→2 (5), 3→4…
- Floyd–Warshall runs on vertices 1 to 4 with these arcs u→v (weight): 1→2 (3), 1→4 (9), 2→3 (6), 2→4 (10), 3→1 (12), 3→2 (6), 4→1 (12), 4→2…
- A tree has the vertices F, H, L, N, O, T, U, V, X and the edges L–O, O–F, L–N, F–H, L–V, O–X, H–U, F–T. A centroid is a vertex whose…
- An undirected graph has the vertices E, F, G, I, Z and the edges G–I, G–Z, G–F, E–F, I–Z, I–F, Z–F. A walk may repeat vertices and edges…
- An undirected graph on 5 vertices has the edges D–E, D–J, D–T, E–H, E–J, E–T, H–T, J–T. How many simple paths (no repeated vertex) go from…
- The directed acyclic graph has the vertices D, F, G, I, J, O, Z and the arcs J→D, I→G, G→F, I→Z, I→D, F→D, G→D. How many distinct directed…