## fizzbuzzer.com

### Looking for good programming challenges?

Use the search below to find our solutions for selected questions!

# Counting Inversions Challenge

Sharing is caring!

Problem Statement
In an array, $arr$, the elements at indices $i$ and $j$ (where $i < j$) form an inversion if $arr\left[i\right] < arr\left[j\right]$. In other words, inverted elements $arr\left[i\right]$ and $arr\left[j\right]$ are considered to be “out of order”. To correct an inversion, we can swap adjacent elements.

For example, consider the dataset $arr=\left[2,4,1\right]$. It has two inversions: $(4,1)$ and $(2,1)$. To sort the array, we must perform the following two swaps to correct the inversions:

Given $d$ datasets, print the number of inversions that must be swapped to sort each dataset on a new line.

Function Description
Complete the function countInversions in the editor below. It must return an integer representing the number of inversions required to sort the array.

countInversions has the following parameter(s):
* arr: an array of integers to sort.

Input Format
The first line contains an integer, $d$, the number of datasets.

Each of the next $d$ pairs of lines is as follows:
1. The first line contains an integer, $n$, the number of elements in $arr$.

1. The second line contains $n$ space-separated integers, $arr\left[i\right]$.

Constraints
* $1 \le d \le 15$
* $1 \le n \le 10^5$
* $1 \le arr\left[i\right] \le 10^7$

Output Format
For each of the $d$ datasets, return the number of inversions that must be swapped to sort the dataset.

Sample Input 1

Sample Output 1

Explanation
We sort the following $d=2$ datasets:
1. $arr=\left[1,1,1,2,2\right]$ is already sorted, so there are no inversions for us to correct. Thus, we print $0$ on a new line.
2. $arr=\left[2,1,3,1,2\right] \rightarrow 1 swap \rightarrow arr=\left[1,2,3,1,2\right]\rightarrow 2 swaps\rightarrow arr=\left[1,1,2,3,2\right]\rightarrow 1 swap\rightarrow arr=\left[1,1,2,2,3\right]$
We performed a total of $1+2+1=4$ swaps to correct inversions.

Sample Input 2

Sample Output 2

Solution
I will use Sample Input 2 as my solution sample input. The solution can be found in $\mathcal{O}(nlogn)$ time.

The way I approach this problem is by first implementing the Merge Sort algorithm to sort the input array. The recursion tree for $arr=\left[2,1,0,3,0\right]$ looks as follows:

Starting from the bottom of the tree, once the array can no longer be split, the arrays (left and right in red color) will be merged at each level of the tree:

Comparing each of the left and right sorted array (red) at each level we can see that the number of swaps we need to make is:
“the sum of counts of right items that are smaller than the current left item for each left item”. For example, comparing $\left[0,1,2\right]$ and $\left[0,3\right]$ we count $1$ for left item $1$ since $1 > 0$. We also count $1$ for left item $2$ since $2 > 0$. This is because we need to swap those pairs in order to sort the array. We do this at all levels to calculate the number of total swaps.

We can do this count during the merging of or left and right array. What we do is follows:
1. Once we stumble upon a left item that is larger than our right item, we set a flag that we need to start counting.
2. If the left item is again less than or equal to a right item and we started our count earlier, then the number of items smaller than our previous left (the one were we started our count) must be equal to the current rightCursor in the right array. The later is because we know the left/right arrays are sorted and any left item $left\left[leftCursor\right] > right\left[rightCursor\right]$ will also be $left\left[leftCursor\right] > right\left[rightCursor-1\right] > right\left[rightCursor-2\right] > \dots > right\left[0\right]$. The same goes for any subsequent left item that is $left\left[leftCursor\right] > lastLeftItemForWhichWeCounted$ (See comparison $\left[0,1,2\right]$ and $\left[0,3\right]$).

Finally, for any items left in the left array, we need to add $sizeof(right)$, since each one of these left items is greater than all items in the right array.

Code