Data Structures & AlgorithmsModerateTCS NQT Digital · Infosys

Problem: A sorted list of distinct integers is 3, 8, 12, 17, 21, 26, 30. How many pairs (i < j) have a[j] − a[i] exactly equal to 9?

  1. A. 6
  2. B. 4
  3. C. 7
  4. D. 5

Correct answer: D. 5

Brute force over pairs of the sorted list checks each difference. Using a two-pointer sweep gives the same count on the sorted array.

More Coding Interview Problems questions

Exams that ask this topic