Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

Consider this recursive function:

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

Correct answer: B. 94

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic