Data Structures & AlgorithmsHardGATE CS · TCS NQT

How many index pairs i < j in [4, 1, 5, 3, 2, 6] satisfy 0 < a[i] - a[j] <= 2?

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

Correct answer: B. 4

Count inversions whose gap is at most 2 with a merge-sort or BIT variant, or check each pair directly. The total inversion count is larger, and the gap filter removes most of them.

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

Exams that ask this topic