Data Structures & AlgorithmsHardGATE CS · ICPC regional
Consider this function:
Consider this function:
def g(n):
if n < 3:
return n
if n % 2 == 0:
return g(n - 3) + g(n - 2)
return g(n - 3) - 1What value does g(12) return?- A. 2
- B. 11
- C. 6
- D. 7
Correct answer: C. 6
Evaluate by recursion: the even branch adds two calls and the odd branch subtracts 1, which gives 6. Swapping the parity test gives 2, subtracting instead of adding gives 2, and dropping the −1 gives 11.
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
- Grid with 4 rows and 4 columns (top to bottom). A cell marked G is a gate; '.' is a normal cell: ..GG ..G. .G.. ..G. Each step, which is a…
- An undirected graph has the vertices C, D, F, K, W, Y and the edges D–W, D–F, D–Y, D–K, W–Y, C–Y, C–K, F–Y, Y–K. What is its chromatic…
- A flow network has source S and sink T, with directed arcs and capacities in brackets: S→D (9), S→B (6), D→B (9), D→T (4), E→D (3), E→T…
- A flow network has source S and sink T, with directed arcs and capacities in brackets: S→B (7), S→T (6), A→B (5), A→T (3), E→B (2), B→C…
- Starting from the integer 3, one operation either adds 1, subtracts 1, or doubles the current value. Values may be any non-negative…
- A pile holds p stones. In one move a player removes exactly s stones, where s is one of {4, 5, 6} and s ≤ the pile size. The player who…