Data Structures & AlgorithmsHardGATE CS · Codeforces Div 2 B
Consider this recursive function:
Consider this recursive function:
def f(n):
if n <= 1:
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. 9
- B. 63
- C. 35
- D. 10
Correct answer: A. 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 on 6 vertices has the edges J–Y, J–K, Y–K, Y–P, K–T, T–P, T–C, P–C. How many simple paths (no repeated vertex) go from…
- A directed acyclic graph has the vertices D, E, L, M, N, X, Z and the edges L→X, L→N, L→M, D→L, M→N, M→E, Z→E. A topological order lists…
- A tree has vertices C, I, M, R, S, U, X and weighted edges (weights in brackets): M–C (9), C–U (2), C–I (8), C–X (1), M–R (8), U–S (7)…
- A directed weighted graph has the arcs u→v (weight): J→Z (11), J→G (6), T→H (1), Z→J (8), Z→S (3), Z→G (3), S→J (2), S→T (12), S→G (2)…
- A counter starts at 1. Each step, you may add 1, add 2, or double the counter. Each step must stay at or below 15. Two sequences of steps…
- A pile holds p stones. In one move a player removes exactly s stones, where s is one of {1, 2, 4} and s ≤ the pile size. The player who…