Data Structures & AlgorithmsHardFlipkart/Uber OA · placement

Two pointers start at both ends of the sorted array [3, 4, 5, 6, 8, 17, 18, 23, 26, 27]. How many pairs (i < j) have a[i] + a[j] ≤ 40? (When a[lo] + a[hi] ≤ target, all pairs (lo, lo+1..hi) qualify.)

  1. A. 1
  2. B. 36
  3. C. 37
  4. D. 38

Correct answer: C. 37

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

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

Exams that ask this topic