Data Structures & AlgorithmsHardCodeforces Div 2 D · ICPC regional
Consider this recursive function:
Consider this recursive function:
def f(n):
if n <= 1:
return
f(n - 2)
if n % 2 == 0:
f(n - 3)How many times is f called in total, including the initial call, when f(10) is made?- A. 31
- B. 6
- C. 16
- D. 17
Correct answer: D. 17
Each call makes one call to f(n − 2) and a second call only when n is divisible by 2; counting every invocation gives 17. Forgetting the divisibility test gives 31, and counting only the calls that are not the initial one gives 16.
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 E, G, I, L and the edges G–I, G–E, I–L, L–E. A walk may repeat vertices and edges. How many walks of…
- A directed acyclic graph has the vertices D, F, I, P, R, X, Y and the directed edges I→F, Y→F, R→P, X→P, Y→D, D→P, X→D, R→F, Y→X. How many…
- Consider this recursive function: python 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…
- 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…