Data Structures & AlgorithmsVery hardCodeforces Div 2 C · GATE CS

Consider this recursive function:

Consider this recursive function:
def f(n):
    if n <= 0:
        return
    f(n - 2)
    if n % 3 == 0:
        f(n - 2)
How many times is f called in total, including the initial call, when f(9) is made?
  1. A. 14
  2. B. 16
  3. C. 15
  4. D. 20

Correct answer: C. 15

Each call makes one call to f(n − 2) and a second call only when n is divisible by 3; counting every invocation gives 15. Forgetting the divisibility test gives 63, and counting only the calls that are not the initial one gives 14.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic