Data Structures & AlgorithmsHardGATE CS · ICPC regional
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].
- A. 28
- B. 5
- C. 17
- D. 56
Correct answer: A. 28
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 [3, 12, 8, 10, 2, 9, 4, 11, 7, 6, 5, 1]: pairs of positions i < j with p[i] > p[j].
- What is the length of the longest strictly increasing subsequence (not necessarily contiguous) of [3, 9, 11, 8, 12, 13, 1, 14, 5, 7, 4…
- What is the length of the longest strictly increasing subsequence (not necessarily contiguous) of [1, 11, 4, 5, 10, 2, 8, 3, 7, 12, 13, 6…
- Elements 0 to 6 start as singletons. For each call union(a, b), in order: let ra and rb be the roots of a and b. If ra = rb nothing…
- Elements 0 to 5 start as singletons. For each call union(a, b), in order: let ra and rb be the roots of a and b. If ra = rb nothing…
- Elements 0 to 7 start as singletons. For each call union(a, b), in order: let ra and rb be the roots of a and b. If ra = rb nothing…