Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS
Consider this function:
Consider this function:
def g(n):
if n < 2:
return n
if n % 2 == 0:
return g(n - 3) + g(n - 3)
return g(n - 3) - 1What value does g(9) return?- A. 2
- B. 0
- C. -3
- D. 1
Correct answer: C. -3
Evaluate by recursion: the even branch adds two calls and the odd branch subtracts 1, which gives -3. Swapping the parity test gives -2, subtracting instead of adding gives -1, and dropping the −1 gives 0.
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 tree has vertices B, H, L, V, W, X, Y and weighted edges (weights in brackets): L–H (7), L–B (2), L–V (3), L–Y (7), L–X (3), X–W (1)…
- A tree has vertices C, G, I, L, M, R, X and weighted edges (weights in brackets): R–X (6), R–I (2), X–L (9), R–M (7), R–C (1), C–G (8)…
- Cities E, P, U, V, Y are joined by roads with these travel costs: V–E 6, V–P 20, V–U 9, V–Y 20, E–P 2, E–U 20, E–Y 11, P–U 15, P–Y 11, U–Y…
- Consider this function: python def g(n): if n < 3: return n if n % 2 == 0: return g(n - 3) + g(n - 2) return g(n - 3) - 1 What value does…
- 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…