Data Structures & AlgorithmsHardCodeforces Div 2 D · ICPC regional

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 - 1) - 1
What value does g(14) return?
  1. A. 10
  2. B. 9
  3. C. 8
  4. D. 16

Correct answer: B. 9

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic