Data Structures & AlgorithmsHard

In the sorted array 1 3 4 7 9 12 14, how many index pairs i < j satisfy A[j] − A[i] ≥ 5?

  1. A. 11
  2. B. 10
  3. C. 14
  4. D. 13

Correct answer: D. 13

In a sorted array, once A[j] − A[i] ≥ d for some i, every larger j also qualifies, so a two-pointer sweep counts the qualifying j in O(1) per i. Reversing the inequality or using a strict bound counts a different set.

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

Exams that ask this topic