Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS
Consider this function:
Consider this function:
def g(n):
if n < 3:
return n
if n % 2 == 0:
return g(n - 3) + g(n - 3)
return g(n - 1) - 1What value does g(8) return?- A. 3
- B. 0
- C. 2
- D. 4
Correct answer: C. 2
Evaluate by recursion: the even branch adds two calls and the odd branch subtracts 1, which gives 2. Swapping the parity test gives -3, subtracting instead of adding gives 0, and dropping the −1 gives 4.
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 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 D, I, K, N, O, R, T, Y and weighted edges (weights in brackets): Y–T (4), Y–I (2), Y–O (8), Y–K (3), O–N (5), O–R (7)…
- An undirected graph has the vertices C, O, Q, W, X, Z and the edges X–Q, Q–C, Q–O, W–C, X–C, O–Z, Q–Z, C–O, X–Z. What is the minimum…
- An undirected graph has the vertices C, G, T, W, X and the edges C–T, C–G, W–T, W–G, W–X, T–G, G–X. A walk may repeat vertices and edges…
- A grid has 6 rows and 6 columns (top to bottom; '.' open, '#' blocked): ..#... .....# .##... ...... ..#..# ...... You may only move right…
- A counter starts at 1. Each step, you may add 1, add 2, or double the counter. Each step must stay at or below 20. Two sequences of steps…