GATE Computer ScienceHardGATE CS (PYQ pattern)
The maximum and the minimum of n numbers are found by divide and conquer: a list of 1 element needs 0 comparisons, 2 elements need 1; a larger list is split into ⌈n/2⌉ and ⌊n/2⌋ elements, solved recursively, and combined with 2 comparisons. How many comparisons does this make for n = 100?
- A. 148
- B. 162
- C. 150
- D. 198
Correct answer: B. 162
C(3) = C(2) + C(1) + 2 = 3, C(4) = 4, C(6) = 8, C(7) = 9, C(12) = 18, C(13) = 19, C(25) = 39, C(50) = 80, C(100) = 162. The pairing method needs only 3n/2 − 2 = 148: divide and conquer reaches that only when n is a power of 2, and splits producing odd parts (25, 13, 7, 3) waste comparisons.
More Algorithms questions
- The strings X = XYXZYXZ and Y = YZXYXZY are given. Their longest common subsequences all have length 5. How many distinct strings of…
- The edit distance between PNEUMONIA and AMMONIA is computed with insertion, deletion and substitution each costing 1. What is the minimum…
- Matrices A₁ … A₇ have dimensions 5×12, 12×7, 7×20, 20×4, 4×15, 15×9 and 9×6, i.e. the dimension array p = [5, 12, 7, 20, 4, 15, 9, 6]…
- 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)…
- 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…