
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Inversion in an Array Using C++
The inverse count of an array indicates how far or how close the array is from being sorted. If the array is already sorted, then the inverse count is zero, but if the array is sorted in reverse order, then the inverse count is maximum.
Here we have given an integer array of size n and need to find the inversions in the array. If two array elements arr[i] and arr[j] form an inversion if arr[i]>arr[j] and i<j. We have to write code to count inversion in an array using C++.
Input / Output Scenario
Let's see the following input output scenario:
Input: arr[] = {6, 4, 2, 1} Output: 6

Input: arr[] = {1, 2, 3, 4} Output: 0
In the above scenario, there is no pair of indices (i, j) that exists in the given array such that arr[i]>arr[j] and i<j.
Count Inversion in an Array Using Naive Approach
For each index in the array, we will count how many smaller elements are present on its right side. Then we will use a nested loop to calculate the inversion count and return the total.
Example
In the following example, we use the naive approach in C++ to count the inversions in the array -
#include <iostream> #include <vector> using namespace std; // Function to count inversions in the array int inversionCount(vector < int > & arr) { int n = arr.size(); int invCount = 0; // Loop through the array for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (arr[i] > arr[j]) invCount++; } } return invCount; } int main() { vector < int > arr = {6, 4, 2, 1}; cout << inversionCount(arr) << endl; return 0; }
Following is the output of the above code -
6
Count Inversion in an Array Using Merge Sort
We can use merge sort to count the number of inversions in an array efficiently in O(n log n) time and O(n) space. First, we split the array into two halves: the left half and the right half. Then, we recursively count the number of inversions in each half. While merging the two halves back together, we also count the number of inversions that happen between the left and right halves (when an element from the left half is greater than one from the right half). Finally, we add up the inversions from the left, right, and between the halves to get the total number of inversions in the array.
Example
In the following example, we use the merge sort in C++ to count the inversions in the array -
#include <iostream> #include <vector> using namespace std; int countAndMerge(vector < int > & arr, int left, int mid, int right) { int n1 = mid - left + 1, n2 = right - mid; // left half vector vector < int > L(n1); // right half vector vector < int > R(n2); for (int i = 0; i < n1; i++) L[i] = arr[i + left]; for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j]; // Initialize inversion count and merge two halves int res = 0; int i = 0, j = 0, k = left; while (i < n1 && j < n2) { if (L[i] <= R[j]) arr[k++] = L[i++]; else { arr[k++] = R[j++]; res += (n1 - i); } } // Merge remaining elements while (i < n1) arr[k++] = L[i++]; while (j < n2) arr[k++] = R[j++]; return res; } // Function to count inversions in the array int countInv(vector < int > & arr, int left, int right) { int res = 0; if (left < right) { int mid = (right + left) / 2; // Recursively count inversions in left and right halves res += countInv(arr, left, mid); res += countInv(arr, mid + 1, right); // Count split inversions res += countAndMerge(arr, left, mid, right); } return res; } int inversionCount(vector < int > & arr) { int n = arr.size(); return countInv(arr, 0, n - 1); } int main() { vector < int > arr = {6, 4, 2, 1}; cout << inversionCount(arr); return 0; }
Following is the output of the code ?
6