Data Structures & AlgorithmsModerateICPC regional · Amazon SDE OA

How many index pairs i < j in [4, 9, 1, 7, 3, 8, 2] have a sum s with 6 <= s <= 10?

  1. A. 11
  2. B. 9
  3. C. 8
  4. D. 7

Correct answer: C. 8

Sort, then count pairs with sum at most R minus pairs with sum at most L-1 using two pointers; this avoids the index-order bookkeeping. The bounds are inclusive on both ends.

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

Exams that ask this topic