This question evaluates understanding of inversion counting in permutations, testing algorithmic problem-solving, complexity analysis, and data-structure reasoning about pairwise order relations.
Given an array a of length n that is a permutation of distinct integers (e.g., 1..n), define an inversion as a pair of indices (i, j) such that:
0 ≤ i < j < n
, and
a[i] > a[j]
.
Write a function that returns the number of inversions in the array.
a
with all distinct values
O(n^2)
approach is fine), but you may also discuss faster approaches if you know them.