Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA

Sorted array a = [1, 1, 2, 2, 2, 3, 4, 4]. How many index pairs (i < j) satisfy a[i] + a[j] = 5? Count positions, not distinct value pairs.

  1. A. 2
  2. B. 7
  3. C. 8
  4. D. 14

Correct answer: B. 7

Duplicates multiply counts: the two 1s pair with the two 4s for 2 x 2 = 4 index pairs, and the three 2s pair with the single 3 for 3 index pairs, giving 7. Counting distinct value pairs gives only 2.

More Coding Interview Problems questions

Exams that ask this topic