Data Structures & AlgorithmsVery hardCodeforces Div 2 C · GATE CS
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(9) is made?- A. 14
- B. 16
- C. 15
- D. 20
Correct answer: C. 15
Each call makes one call to f(n − 2) and a second call only when n is divisible by 3; counting every invocation gives 15. Forgetting the divisibility test gives 63, and counting only the calls that are not the initial one gives 14.
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 counter starts at 1. Each step, you may add 1, add 2, or double the counter. Each step must stay at or below 22. Two sequences of steps…
- Starting from the integer 9, one operation either adds 1, subtracts 1, or doubles the current value. Values may be any non-negative…
- An undirected graph has the vertices A, O, S, T, V, Y and the edges Y–V, T–A, T–V, O–T, O–A, Y–A, S–V, O–S, V–A. What is the minimum…
- An undirected graph has the vertices M, O, U, Y and the edges U–O, U–Y, M–O, O–Y. In how many ways can each vertex be given one of 3…
- The directed acyclic graph has the vertices B, C, K, P, Q, R, V, Z and the arcs Z→V, K→R, C→B, K→C, Q→B, Z→B, Z→K, R→V, Q→Z, P→R, K→B…
- A directed graph has the vertices C, D, F, G, H, I, Q, W and the arcs I→H, I→D, I→F, I→W, Q→D, Q→F, Q→G, Q→C, H→F, H→W, H→C, F→I, F→Q…