Data Structures & AlgorithmsHardGATE CS · ICPC regional

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(11) is made?
  1. A. 15
  2. B. 127
  3. C. 17
  4. D. 16

Correct answer: D. 16

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic