Data Structures & AlgorithmsHardGATE CS · DSA

Two pointers start at both ends of the sorted array [2, 5, 10, 11, 12, 14, 16, 18, 21, 29]. How many pairs (i < j) have a[i] + a[j] ≤ 28? (When a[lo] + a[hi] ≤ target, all pairs (lo, lo+1..hi) qualify.)

  1. A. 26
  2. B. 25
  3. C. 2
  4. D. 23

Correct answer: B. 25

If a[lo] + a[hi] ≤ 28, every j in (lo, hi] also works with lo, so add hi − lo and advance lo; otherwise decrement hi. The total is 25 (of which 2 pairs sum to exactly 28).

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic