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)?
- A. 139
- B. 119
- C. 69
- 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.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- Elements 1, 2, 3, 4, 5, 6 are pushed in this order onto a stack, with pops allowed at any time until all are popped. Of all possible…
- An input-restricted double-ended queue lets items 1, 2, 3, 4, 5 be inserted only at the rear (in that order) while removals are allowed at…
- A hash table has 7 slots (0 to 6) and uses linear probing with h(k) = k mod 7. The six keys 8, 15, 3, 10, 5, 12 are inserted in some…
- The distinct keys 1..8 are arranged as a max-heap in a 0-indexed array of size 8 (children of i at 2i+1, 2i+2). Over all valid max-heaps…
- Heapsort (max-heap, in-place) is applied to [9, 4, 7, 1, 8, 2, 6, 3, 5, 10, 12, 11, 15, 14, 13]. First the array is made a max-heap with…
- Keys 50, 30, 70, 20, 40, 60, 80, 10, 25, 35, 45, 55, 65, 75, 85, 5, 12, 22, 27, 90 are inserted in this order into an empty BST (root at…