Data Structures & AlgorithmsVery hardTCS NQT · Infosys

How many index pairs i < j in [1, 3, 5, 7, 9, 4] have |a[i] - a[j]| = 2?

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

Correct answer: B. 4

The pairs with difference 2 are (1,3), (3,5), (5,7) and (7,9). The value 4 has neither 2 nor 6 in the array, so it adds no pair.

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

Exams that ask this topic