Data Structures & AlgorithmsModerateGATE CS · ICPC regional

Count the inversions in the permutation [3, 12, 8, 10, 2, 9, 4, 11, 7, 6, 5, 1]: pairs of positions i < j with p[i] > p[j].

  1. A. 84
  2. B. 42
  3. C. 7
  4. D. 24

Correct answer: B. 42

An inversion is any pair (not just adjacent ones) with a larger value on the left. Adjacent descents, the non-inversion pairs, and double-counting each pair are the three common misreadings.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic