ProgrammingVery hardCompetitive coding (CP)

What is the maximum value of depth printed?

What is the maximum value of depth printed?
#include <stdio.h>
int depth = 0, maxd = 0;
void r(int n) {
    depth++;
    if (depth > maxd) maxd = depth;
    if (n > 0) {
        r(n - 1);
        r(n - 2);
    }
    depth--;
}
int main(void) {
    r(4);
    printf("%d", maxd);
}
  1. A. 4
  2. B. 6
  3. C. 5
  4. D. 3

Correct answer: C. 5

The deepest chain goes r(4), r(3), r(2), r(1), r(0), which has five active calls. Counting only the recursive calls gives 4, and the other branches are never deeper.

More C Programming questions

Exams that ask this topic