Data Structures & AlgorithmsHardCodeforces Div 2 C · ICPC regional

Array a = [9, 4, 7, 1, 8, 2, 6, 3]. How many index pairs i < j have |a[i] - a[j]| >= 5?

  1. A. 12
  2. B. 7
  3. C. 10
  4. D. 6

Correct answer: C. 10

Sorting the values lets you count pairs whose gap is at least 5 with two pointers: for each smaller value, every larger value beyond the threshold pairs with it. A strict bound on the gap drops pairs with gap exactly 5.

More Coding Interview Problems questions

Exams that ask this topic