Data Structures & AlgorithmsModerateGATE CS · TCS NQT

How many index pairs i < j in the array [1, 5, 3, 4, 2, 7, 9] have absolute difference exactly 2?

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

Correct answer: D. 5

For each value x, check whether x+2 and x-2 appear; counting both directions double-counts the same pair.

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

Exams that ask this topic