Data Structures & AlgorithmsHard

How many index pairs i < j in [5, 1, 9, 4, 8, 3, 7] satisfy |A[i] − A[j]| ≤ 3?

  1. A. 8
  2. B. 17
  3. C. 15
  4. D. 11

Correct answer: D. 11

Taking the absolute difference means the order of the two values does not matter, so the check is symmetric. Using a strict bound, dropping the absolute value, or widening the bound each change the count.

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

Exams that ask this topic