Data Structures & AlgorithmsHardGATE CS · TCS NQT

How many inversions (pairs i < j with a[i] > a[j]) does the array [2, 4, 1, 3, 5] contain?

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

Correct answer: B. 3

Pairs (2,1), (4,1) and (4,3) are inversions. Counting adjacent descents gives only 1, which misses the non-adjacent pairs (2,1) and (4,1).

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

Exams that ask this topic