Data Structures & AlgorithmsHardCodeforces Div 2 D · ICPC regional
Consider this function:
Consider this function:
def g(n):
if n < 2:
return n
if n % 2 == 0:
return g(n - 3) + g(n - 2)
return g(n - 3) - 1What value does g(8) return?- A. -3
- B. 2
- C. 1
- D. 0
Correct answer: A. -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 -1.
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 connected undirected graph has the vertices F, H, I, M, O, V and the edges F–M, F–I, F–O, M–V, M–H, H–I. How many bridges does it have…
- A counter starts at 1. Each step, you may add 1, add 2, or double the counter. Each step must stay at or below 10. Two sequences of steps…
- An undirected graph has the vertices C, G, I, K, N, W, Y and the edges C–Y, C–I, C–W, C–K, Y–I, Y–W, Y–K, N–I, N–W, G–K, I–K, W–K. What is…
- A directed weighted graph has the arcs u→v (weight): B→U (10), B→S (15), O→U (1), O→S (9), O→P (7), O→L (11), U→O (8), U→S (3), S→L (2)…
- A tree has the vertices D, G, I, J, M, T, X, Y and the edges Y–I, I–J, J–G, J–X, G–T, X–M, Y–D. A set D dominates the tree if every vertex…
- An undirected graph has the vertices D, E, H, K, L, Q, V, Z and the edges Q–H, Q–V, Q–K, L–Z, L–D, E–V, E–K, Z–V, Z–K, H–K, V–D. What is…