Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2 B
Consider this recursive function:
Consider this recursive function:
def f(n):
if n <= 1:
return
f(n - 1)
if n % 2 == 0:
f(n - 3)How many times is f called in total, including the initial call, when f(8) is made?- A. 22
- B. 21
- C. 37
- D. 16
Correct answer: B. 21
Each call makes one call to f(n − 1) and a second call only when n is divisible by 2; counting every invocation gives 21. Forgetting the divisibility test gives 37, and counting only the calls that are not the initial one gives 20.
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 24. Two sequences of steps…
- Floyd–Warshall runs on vertices 1 to 4 with these arcs u→v (weight): 1→4 (4), 2→1 (1), 2→3 (4), 3→1 (5), 3→4 (10), 4→1 (6), 4→2 (6), 4→3…
- A counter starts at 1. Each step, you may add 1, add 2, or double the counter. Each step must stay at or below 17. Two sequences of steps…
- A grid has 8 rows and 5 columns (rows listed top to bottom; '.' is an open cell, '#' a wall): .#.## ..... #...# .#... ..... ..##. ....#…
- A tree has the vertices D, G, O, Q, T, Y, Z and the edges T–Y, Y–G, Y–D, G–Q, T–Z, Z–O. What is the minimum number of vertices needed so…
- A tree has vertices with values in brackets: J[0], N[0], I[8], Q[3], K[-2]. The edges are J–N, N–I, N–Q, I–K. A path is any sequence of…