Given an integer array arr[] of size n, find the inversioncount in the array. Two array elements arr[i] and arr[j] form an inversion if arr[i] > arr[j] and i < j.
Note: Inversion Countfor an array indicates that how far (or close) the array is from being sorted. If the array is already sorted, then the inversion count is 0, but if the array is sorted in reverse order, the inversion count is maximum.
[Naive Approach] Using Two Nested Loops - O(n^2) Time and O(1) Space
The main idea of this approach is to count the number of inversions in an array by using nested loops, checking every possible pair of elements and counting how many such pairs satisfy the inversion condition. The outer loop starts from the first element and goes up to the second-to-last element, and for each i, the inner loop runs from the next element (i + 1) to the end of the array. This ensures that all pairs (i, j) where i < j are checked. Inside the inner loop, it checks if arr[i] > arr[j], if so, it means there is an inversion, and the invCount is incremented.
Output
6
[Expected Approach] Using Merge Step of Merge Sort - O(n*log n) Time and O(n) Space
We can use merge sort to count the inversions in an array. First, we divide the array into two halves: left half and right half. Next, we recursively count the inversions in both halves. While merging the two halves back together, we also count how many elements from the left half array are greater than elements from the right half array, as these represent cross inversions (i.e., element from the left halfof the array is greater than an element from the right half during the merging process in the merge sort algorithm). Finally, we sum the inversions from the left half, right half, and the cross inversions to get the total number of inversions in the array. This approach efficiently counts inversions while sorting the array.
Let's understand the above intuition in more detailed form, as we get to know that we have to perform the merge sort on the given array. Below images represents dividing and merging steps of merge sort.
During each merging step of the merge sort algorithm, we count cross inversions by comparing elements from the left half of the array with those from the right half. If we find an element arr[i] in the left half that is greater than an element arr[j] in the right half, we can conclude that all elements after i in the left half will also be greater than arr[j]. This allows us to count multiple inversions at once. Let's suppose if there are k elements remaining in the left half after i, then there are k cross inversions for that particular arr[j]. The rest of the merging process continues as usual, where we combine the two halves into a sorted array. This efficient counting method significantly reduces the number of comparisons needed, enhancing the overall performance of the inversion counting algorithm.
Below Illustration represents the cross inversion of a particular step during the merging process in the merge sort algorithm:
Output
6
Note: The above code modifies (or sorts) the input array. If we want to count only inversions, we need to create a copy of the original array and perform operation on the copy array.