Data Structures & AlgorithmsModerateCodeforces Div 2 C · ICPC regional

Sorted array a = [1, 3, 4, 7, 10, 12, 15]. How many pairs of indices i < j satisfy a[j] - a[i] <= 3?

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

Correct answer: B. 7

For each left index, the valid right indices form a contiguous block that starts at i+1 and stops when the gap exceeds d. A moving right pointer counts the block in linear total time. A strict bound misses gaps equal to exactly 3, such as (1, 4) and (4, 7).

More Coding Interview Problems questions

Exams that ask this topic