GATE Computer ScienceModerateGATE CS (PYQ pattern)

int f(int a[], int l, int r) { if (l == r) return a[l]; int m = (l + r) / 2; int x = f(a, l, m), y = f(a, m + 1, r); return x > y ? x : y; } How many calls to f (including the first) are made by f(a, 0, 22)?

  1. A. 45
  2. B. 23
  3. C. 46
  4. D. 47

Correct answer: A. 45

The recursion tree has one leaf per element (23) and every internal node has exactly 2 children, so there are 23 leaves + 22 internal nodes = 45 calls.

More Programming and Data Structures (GATE) questions

Exams that ask this topic