Solution: Number of Inversions
Solve the Number of Inversions Problem.
Solution
Let’s try the most frequently used type of the divide-and-conquer strategy. Split the input sequence into two halves, and , and make a recursive call for each of them. This allows us to compute all inversions that lie in the same half. But it does not reveal the numbers of split inversions, i.e., the number of pairs such that lies in the left half, lies in the right half, and .
Stop and think: Consider an element in . What is the number of split inversions that belongs to?
Given an array and an integer , let be the number of elements in that are smaller than . Since the answer to the question above is , our goal is to rapidly compute .
It is equal to the number of elements in ...