Data Structures & AlgorithmsHardGATE CS · ICPC regional

Consider this function:

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

Correct answer: C. -5

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic