Data Structures & AlgorithmsHard

How many index pairs i < j in 1 4 1 4 4 2 1 2 2 hold EQUAL values and satisfy j − i ≤ 2?

  1. A. 5
  2. B. 9
  3. C. 2
  4. D. 10

Correct answer: A. 5

Compare each position only with the next two positions, which keeps the check local; the distance bound is what restricts the pairs. Dropping the bound, tightening it to one, or counting unequal pairs changes the answer.

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

Exams that ask this topic