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(11) is made?- A. 15
- B. 127
- C. 17
- D. 16
Correct answer: D. 16
Each call makes one call to f(n − 2) and a second call only when n is divisible by 3; counting every invocation gives 16. Forgetting the divisibility test gives 127, and counting only the calls that are not the initial one gives 15.
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 D, P, Q, Y and the edges Y–P, Y–D, P–D, Q–D. A walk may repeat vertices and edges. How many walks of…
- A bipartite graph has left side P1, P2, P3 and right side Q1, Q2, Q3, Q4, with edges P2–Q1, P2–Q3, P1–Q4, P1–Q3, P2–Q4, P3–Q2. What is the…
- A four-wheel combination lock shows four digits 0–9 on wheels that wrap around (9 goes to 0 and 0 goes to 9). One move turns one wheel by…
- A tree has vertices with values in brackets: X[-1], L[8], G[-1], S[7], F[8], K[6]. The edges are X–L, L–G, X–S, X–F, S–K. A path is any…
- An undirected graph has the vertices A, G, H, L, P and the edges L–G, H–P, A–H, H–G, H–L, A–P, P–G. How many simple cycles does it contain…
- A grid has 4 rows and 7 columns (top to bottom; '.' open, '#' blocked): .#.##.. ..#..#. .....#. ....... You may only move right or down…