GATE Computer ScienceVery hardGATE CS (PYQ pattern)

How many pairs of positions i < j in the sorted array [1, 3, 4, 6, 7, 9, 11, 14, 15, 19] have a[i] + a[j] ≤ 20?

  1. A. 27
  2. B. 29
  3. C. 31
  4. D. 33

Correct answer: B. 29

Two pointers: for each i, the largest j with a[i] + a[j] ≤ 20 counts all partners between them: 9, 7, 6, 4, 2, 1, 0… summing the per-element counts gives 29 pairs in O(n) time.

More Algorithms questions

Exams that ask this topic