Data Structures & AlgorithmsHardGATE CS · ICPC regional
Consider this recursive function:
Consider this recursive function:
def f(n):
if n <= 0:
return
f(n - 2)
if n % 3 == 0:
f(n - 2)How many times is f called in total, including the initial call, when f(10) is made?- A. 35
- B. 8
- C. 9
- D. 63
Correct answer: C. 9
Each call makes one call to f(n − 2) and a second call only when n is divisible by 3; counting every invocation gives 9. Forgetting the divisibility test gives 63, and counting only the calls that are not the initial one gives 8.
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
- An undirected graph has the vertices K, L, N, P, Q, R and the edges P–K, K–N, R–Q, R–N, N–L, N–Q, K–L, P–N. How many simple cycles does it…
- A tree has the vertices A, D, E, L, P, V, X and the edges X–D, X–P, X–E, D–V, E–L, P–A. What is the minimum number of vertices needed so…
- An undirected graph on 6 vertices has the edges H–T, H–Y, H–X, T–W, T–F, Y–F, W–F, W–X, F–X. How many simple paths (no repeated vertex) go…
- Each cell of a grid with 5 rows and 3 columns has an arrow: R = right, L = left, U = up, D = down. URU LDU DRL RLD DUU From a cell you may…
- An undirected graph has the vertices B, C, D, E, I, M, R, S, V and the edges S–B, I–M, I–V, R–M, R–D, R–V, R–C, D–C, V–C, E–B, B–C. What…
- An undirected graph has the vertices C, D, K, M, Q, V and the edges Q–D, Q–M, C–D, K–D, K–V, D–M, D–V. What is the size of a maximum…