Data Structures & AlgorithmsHardGATE CS · Codeforces Div2 D

How many inversions does the array [2, 4, 1, 3, 5] have? An inversion is a pair of positions i < j with a[i] > a[j].

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

Correct answer: C. 3

Count inversions with merge sort: while merging two sorted halves, every time an element from the right half is placed first, it forms inversions with all remaining left elements. Counting only adjacent descents gives a different number.

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

Exams that ask this topic