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 - 2)
return g(n - 1) - 1What value does g(12) return?- A. 14
- B. 21
- C. 15
- D. 6
Correct answer: A. 14
Evaluate by recursion: the even branch adds two calls and the odd branch subtracts 1, which gives 14. Swapping the parity test gives 6, subtracting instead of adding gives -10, and dropping the −1 gives 21.
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
- Consider this recursive function: python def f(n): if n <= 1: return f(n - 1) if n % 2 == 0: f(n - 2) How many times is f called in total…
- A bipartite graph has left side P1, P2, P3, P4 and right side Q1, Q2, Q3, Q4, with edges P1–Q2, P1–Q3, P1–Q1, P2–Q3. What is the size of a…
- A tree has the vertices A, C, D, F, G, M, P, S, X, Y and the edges A–M, A–S, M–Y, A–F, M–D, D–X, Y–C, F–G, S–P. A set D dominates the tree…
- A grid has 5 rows and 7 columns (rows listed top to bottom; '.' is an open cell, '#' a wall): .#..... ....#.. .#####. .#..... ..#…
- A connected undirected graph has the vertices D, H, J, K, L, T and the edges J–L, J–D, J–H, D–K, D–T, K–T, T–H. How many bridges does it…
- A tree has the vertices D, E, G, H, I, T, U, X, Z and the edges D–T, D–H, T–G, H–E, E–X, D–Z, T–U, Z–I. What is the minimum number of…