Data Structures & AlgorithmsHardGATE CS · Codeforces Div 2 B

Consider this function:

Consider this function:
def g(n):
    if n < 3:
        return n
    if n % 2 == 0:
        return g(n - 3) + g(n - 4)
    return g(n - 3) - 1
What value does g(9) return?
  1. A. 0
  2. B. 2
  3. C. 1
  4. D. 3

Correct answer: A. 0

Evaluate by recursion: the even branch adds two calls and the odd branch subtracts 1, which gives 0. Swapping the parity test gives 1, subtracting instead of adding gives -4, and dropping the −1 gives 2.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic