GATE Computer ScienceVery hardGATE CS (PYQ pattern)

int C(int n, int k) { if (k == 0 || k == n) return 1; if (k == 1) return n; return C(n - 1, k - 1) + C(n - 1, k); } How many calls to C (including the first) are made while evaluating C(8, 4)?

  1. A. 139
  2. B. 119
  3. C. 69
  4. D. 70

Correct answer: C. 69

Count calls with T(n, k) = 1 when k == 0, k == n or k == 1, else 1 + T(n-1, k-1) + T(n-1, k). Without the k == 1 shortcut every leaf returns 1 and the tree has 2 * C(8,4) - 1 = 139 nodes. With it, T(n, 2) = 1 + 1 + T(n-1, 2) with T(2, 2) = 1, so T(n, 2) = 2n - 3; then T(n, 3) and T(8, 4) follow by the recurrence: 69 calls.

More Programming and Data Structures (GATE) questions

Exams that ask this topic