Data Structures & AlgorithmsHardGATE CS · TCS NQT

How many index pairs i < j in [3, 4, 4, 5, 7, 6] have values that differ by exactly 1?

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

Correct answer: D. 6

Count the pairs (3,4) twice, (4,5) twice, (5,6) once and (6,7) once; the value 7 pairs with 6 at a later index, which is counted as a separate pair.

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

Exams that ask this topic