# C++ Program for Counting Inversions in an Array

In this article, we will discuss C++ program for counting inversions in an array with several methods.

## What is Inversion Count?

Inversion Count for an array indicates - how far (or close) the array is from being sorted. If the array is already sorted, the inversion count is 0. If the array is sorted in reverse order, that inversion count is maximum.

Let us understand with an example:

Input: arr[] = {2, 4, 1, 3, 5}

Output: 3

There are three inversions in the array:

(2, 1), (4, 1), (4, 3)

## Simple Method for Counting Inversions:

There are several methods for counting inversions for an array. Some main methods for the counting inversions are as follows:

### 1. Brute Force

In the Brute Force, compare every element with all other elements and count pairs that are in reversed order.

Time Complexity: O(n^2)

Illustration with Examples:

Let us illustrate this method with the provided examples:

Example 1:

Input: arr[] = {8, 4, 2, 1}

• In this example, start at 8. These are no elements to the right, so no inversions.
• Move to 4. There are two elements to the right (2 and 1) that are smaller, so there are two inversions.
• Move to 2. There is one element to the right (1) that is smaller, so one inversion.
• Move to 1. There are no elements to the right, so no inversions.
• Total inversions: 0 + 2 + 1 + 0 = 3

Example 2:

Input: arr[] = {3, 1, 2}

• Start at 3, there are two elements to the right (1 and 2) that are smaller, so two inversions.
• Move to 1. There is one element to the right (2) that is smaller, so one inversion.
• Move to 2. There are no elements to the right, so no inversions.
• Total inversions: 2 + 1 + 0 = 3

Algorithm:

Here is a simple approach to count inversions in an array explained in points:

1. Inversions in an array indicate how far the array is from being sorted.
2. An inversion is defined as a pair of elements (arr[i], arr[j]) where i < j but arr[i] > arr[j].
3. A simple brute force approach is to compare every element with all other elements after it.
4. Initialize a variable count = 0 to store the inversion count.
5. Run two nested loops: the outer loop picks element arr[i], and the inner loop compares arr[i] with arr[j] where j varies from i+1 to n-1.
6. If arr[i] > arr[j], an inversion is found, so increment count by 1.
7. Repeat this for each element arr[i] from 0 to n-2.
8. After complete traversal, the count will store the total inversions in the array.
9. The time complexity of this method is O(n^2) since nested traversal of the complete array is done.

So, a simple brute force method is to compare each element with all elements after it and count pairs that are inverted. This quadratic algorithm serves as a basic approach to understanding the inversion counting problem.

Program:

Output:

```Number of inversions: 6
```

### 2. Merge Sort Approach

Use the merge sort method to count inversions. While merging two sorted halves, count split inversions.

Time Complexity: O(nlogn)

Algorithm:

Here is an explanation of counting inversions using Merge Sort:

1. The merge sort algorithm can be modified to count inversions while merging the sorted halves.
2. The main logic is that when two sorted subarrays are merged, each element of the right subarray is compared with the elements of the left subarray.
3. If an element in the right subarray is smaller than an element in the left subarray, this pair forms an inversion.
4. To implement this:
• Divide the array into two halves using merge sort recursive logic.
• Count inversions in the left half and right half recursively.
5. To merge the two sorted halves:
• Initialize indexes i, j for left and right subarrays.
• Initialize count variable to store split inversions.
6. Compare arr[i] and arr[j], if arr[i] > arr[j], an inversion is found.
7. After that, increment count by m-i, where m is the mid index. It gives inversions between remaining unmatched left elements and arr[j].
8. Increment i and j appropriately while merging.
10. Overall time complexity is O(nlogn) since merge sort is used.

Program:

Output:

```Number of inversions: 6
```