Data Structures & AlgorithmsVery hardGATE CS · ICPC regional
Count the inversions in the permutation [8, 2, 7, 4, 3, 5, 9, 11, 10, 6, 1]: pairs of positions i < j with p[i] > p[j].
- A. 50
- B. 6
- C. 30
- D. 25
Correct answer: D. 25
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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Count the inversions in the permutation [5, 9, 10, 4, 1, 6, 8, 3, 7, 2]: pairs of positions i < j with p[i] > p[j].
- Count the inversions in the permutation [9, 4, 10, 11, 2, 6, 7, 3, 5, 1, 8]: pairs of positions i < j with p[i] > p[j].
- Count the inversions in the permutation [6, 1, 9, 7, 4, 2, 5, 3, 10, 8]: pairs of positions i < j with p[i] > p[j].
- Count the inversions in the permutation [4, 9, 6, 2, 1, 11, 5, 7, 8, 3, 10]: pairs of positions i < j with p[i] > p[j].
- Count the inversions in the permutation [2, 9, 7, 4, 10, 8, 5, 6, 1, 3]: pairs of positions i < j with p[i] > p[j].
- 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].