GATE Computer ScienceModerateGATE CS (PYQ pattern)
A recursive matrix-chain function without memoisation, cost(i, j), returns 0 when i = j and otherwise calls cost(i, k) and cost(k+1, j) for every k from i to j − 1. How many calls to cost (including the first one) are made by cost(1, 6)?
- A. 243
- B. 42
- C. 81
- D. 729
Correct answer: A. 243
Let R(n) be the calls for a chain of n matrices: R(1) = 1 and R(n) = 1 + Σ_{k=1}^{n−1} (R(k) + R(n−k)). This gives 1, 3, 9, 27, 81, 243, i.e. R(n) = 3^(n−1), so R(6) = 243. 42 is the number of parenthesisations of 6 matrices; with memoisation only 21 subchains are distinct.
More Algorithms questions
- A 0/1 knapsack has capacity 26 and items (weight, value): (12, 24), (7, 13), (11, 23), (8, 15), (9, 16), (6, 11). The maximum total value…
- A coin system has denominations {1, 5, 10, 20, 25}. The greedy algorithm (always take the largest coin that fits) first gives a…
- For the coin system {1, 5, 12, 25}, among the amounts 1, 2, …, 100, for how many amounts does the greedy algorithm use more coins than the…
- Using unlimited coins of denominations 2, 5, 7 and 10 (order does not matter), how many ways are there to pay exactly 40 using at most 6…
- In the array [2, 5, 3, 7, 11, 8, 10, 13, 6, 9, 4, 12] the longest strictly increasing subsequences have length 6. How many different…
- The O(n log n) LIS algorithm keeps an array t where t[k] is the smallest tail of an increasing subsequence of length k + 1; each element x…