Count Inversions of an Array (original) (raw)
Last Updated : 01 Dec, 2024
Given an integer array **arr[] of size **n, find the **inversion **count 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.
**Examples:
**Input: arr[] = {4, 3, 2, 1}
**Output: 6
**Explanation:
**Input: arr[] = {1, 2, 3, 4, 5}
**Output: 0
**Explanation: There is no pair of indexes (**i, j) exists in the given array such that arr[i] > arr[j] and i < j**Input: arr[] = {10, 10, 10}
**Output: 0
Table of Content
- [Naive Approach] Using Two Nested Loops - O(n^2) Time and O(1) Space
- [Expected Approach] Using Merge Sort - O(n*log n) Time and O(n) Space
**[Naive Approach] Using Two Nested Loops - O(n^2) Time and O(1) Space
Traverse through the array, and for every **index, find the number of smaller elements on its right side in the array. This can be done using a nested loop. Sum up the **inversion counts for all indices in the array and return the **sum.
C++ `
// C++ program to Count Inversions in an array // using nested loop
#include #include using namespace std;
// Function to count inversions in the array int inversionCount(vector &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 the current element is greater
// than the next, increment the count
if (arr[i] > arr[j])
invCount++;
}
}
return invCount;
}
int main() { vector arr = {4, 3, 2, 1}; cout << inversionCount(arr) << endl; return 0; }
C
// C program to Count Inversions in an array // using nested loop
#include <stdio.h>
// Function to count inversions in the array int inversionCount(int arr[], int n) { int invCount = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
// If the current element is greater than the next,
// increment the count
if (arr[i] > arr[j])
invCount++;
}
}
return invCount;
}
int main() { int arr[] = {4, 3, 2, 1}; int n = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", inversionCount(arr, n));
return 0;
}
Java
// Java program to Count Inversions in an array // using nested loop
import java.util.*;
class GfG {
// Function to count inversions in the array
static int inversionCount(int arr[]) {
int n = arr.length;
int invCount = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
// If the current element is greater than the next,
// increment the count
if (arr[i] > arr[j])
invCount++;
}
}
return invCount;
}
public static void main(String[] args) {
int arr[] = {4, 3, 2, 1};
System.out.println(inversionCount(arr));
}
}
Python
Python program to Count Inversions in an array
using nested loop
Function to count inversions in the array
def inversionCount(arr): n = len(arr) invCount = 0
for i in range(n - 1):
for j in range(i + 1, n):
# If the current element is greater than the next,
# increment the count
if arr[i] > arr[j]:
invCount += 1
return invCount
if name == "main": arr = [4, 3, 2, 1] print(inversionCount(arr))
C#
// C# program to Count Inversions in an array // using nested loop using System;
class GfG {
// Function to count inversions in the array
static int inversionCount(int[] arr) {
int n = arr.Length;
int invCount = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
// If the current element is greater than the next,
// increment the count
if (arr[i] > arr[j])
invCount++;
}
}
return invCount;
}
static void Main() {
int[] arr = { 4, 3, 2, 1 };
Console.WriteLine(inversionCount(arr));
}
}
JavaScript
// JavaScript program to Count Inversions in an array // using nested loop
function inversionCount(arr) { let n = arr.length; let invCount = 0;
// Loop through the array
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
// If the current element is greater than the next,
// increment the count
if (arr[i] > arr[j]) {
invCount++;
}
}
}
return invCount;
}
// driver code let arr = [4, 3, 2, 1]; console.log(inversionCount(arr));
`
**[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:
C++ `
// C++ program to Count Inversions in an array using merge sort
#include #include using namespace std;
// This function merges two sorted subarrays arr[l..m] and arr[m+1..r] // and also counts inversions in the whole subarray arr[l..r] int countAndMerge(vector& arr, int l, int m, int r) {
// Counts in two subarrays
int n1 = m - l + 1, n2 = r - m;
// Set up two vectors for left and right halves
vector<int> left(n1), right(n2);
for (int i = 0; i < n1; i++)
left[i] = arr[i + l];
for (int j = 0; j < n2; j++)
right[j] = arr[m + 1 + j];
// Initialize inversion count (or result) and merge two halves
int res = 0;
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
// No increment in inversion count if left[] has a
// smaller or equal element
if (left[i] <= right[j])
arr[k++] = left[i++];
// If right is smaller, then it is smaller than n1-i
// elements because left[] is sorted
else {
arr[k++] = right[j++];
res += (n1 - i);
}
}
// Merge remaining elements
while (i < n1)
arr[k++] = left[i++];
while (j < n2)
arr[k++] = right[j++];
return res;
}
// Function to count inversions in the array int countInv(vector& arr, int l, int r){ int res = 0; if (l < r) { int m = (r + l) / 2;
// Recursively count inversions in the left and
// right halves
res += countInv(arr, l, m);
res += countInv(arr, m + 1, r);
// Count inversions such that greater element is in
// the left half and smaller in the right half
res += countAndMerge(arr, l, m, r);
}
return res;
}
int inversionCount(vector &arr) { int n = arr.size(); return countInv(arr, 0, n-1); }
int main(){ vector arr = {4, 3, 2, 1};
cout << inversionCount(arr);
return 0;
}
C
// C program to Count Inversions in an array using merge sort
#include <stdio.h>
// This function merges two sorted subarrays arr[l..m] and arr[m+1..r] // and also counts inversions in the whole subarray arr[l..r] int countAndMerge(int arr[], int l, int m, int r) {
// Counts in two subarrays
int n1 = m - l + 1, n2 = r - m;
// Set up two arrays for left and right halves
int left[n1], right[n2];
for (int i = 0; i < n1; i++)
left[i] = arr[i + l];
for (int j = 0; j < n2; j++)
right[j] = arr[m + 1 + j];
// Initialize inversion count (or result)
// and merge two halves
int res = 0;
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
// No increment in inversion count
// if left[] has a smaller or equal element
if (left[i] <= right[j])
arr[k++] = left[i++];
// If right is smaller, then it is smaller than n1-i
// elements because left[] is sorted
else {
arr[k++] = right[j++];
res += (n1 - i);
}
}
// Merge remaining elements
while (i < n1)
arr[k++] = left[i++];
while (j < n2)
arr[k++] = right[j++];
return res;
}
// Function to count inversions in the array int countInv(int arr[], int l, int r) { int res = 0; if (l < r) { int m = (r + l) / 2;
// Recursively count inversions
// in the left and right halves
res += countInv(arr, l, m);
res += countInv(arr, m + 1, r);
// Count inversions such that greater element is in
// the left half and smaller in the right half
res += countAndMerge(arr, l, m, r);
}
return res;
}
int inversionCount(int arr[], int n) { return countInv(arr, 0, n - 1); }
int main() { int arr[] = {4, 3, 2, 1}; int n = sizeof(arr) / sizeof(arr[0]); printf("%d", inversionCount(arr, n)); return 0; }
Java
// Java program to Count Inversions in an array using merge sort
import java.util.*;
class GfG {
// This function merges two sorted subarrays arr[l..m] and arr[m+1..r]
// and also counts inversions in the whole subarray arr[l..r]
static int countAndMerge(int[] arr, int l, int m, int r) {
// Counts in two subarrays
int n1 = m - l + 1, n2 = r - m;
// Set up two arrays for left and right halves
int[] left = new int[n1];
int[] right = new int[n2];
for (int i = 0; i < n1; i++)
left[i] = arr[i + l];
for (int j = 0; j < n2; j++)
right[j] = arr[m + 1 + j];
// Initialize inversion count (or result)
// and merge two halves
int res = 0;
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
// No increment in inversion count
// if left[] has a smaller or equal element
if (left[i] <= right[j])
arr[k++] = left[i++];
// If right is smaller, then it is smaller than n1-i
// elements because left[] is sorted
else {
arr[k++] = right[j++];
res += (n1 - i);
}
}
// Merge remaining elements
while (i < n1)
arr[k++] = left[i++];
while (j < n2)
arr[k++] = right[j++];
return res;
}
// Function to count inversions in the array
static int countInv(int[] arr, int l, int r) {
int res = 0;
if (l < r) {
int m = (r + l) / 2;
// Recursively count inversions
// in the left and right halves
res += countInv(arr, l, m);
res += countInv(arr, m + 1, r);
// Count inversions such that greater element is in
// the left half and smaller in the right half
res += countAndMerge(arr, l, m, r);
}
return res;
}
static int inversionCount(int[] arr) {
return countInv(arr, 0, arr.length - 1);
}
public static void main(String[] args) {
int[] arr = {4, 3, 2, 1};
System.out.println(inversionCount(arr));
}
}
Python
Python program to Count Inversions in an array using merge sort
This function merges two sorted subarrays arr[l..m] and arr[m+1..r]
and also counts inversions in the whole subarray arr[l..r]
def countAndMerge(arr, l, m, r):
# Counts in two subarrays
n1 = m - l + 1
n2 = r - m
# Set up two lists for left and right halves
left = arr[l:m + 1]
right = arr[m + 1:r + 1]
# Initialize inversion count (or result)
# and merge two halves
res = 0
i = 0
j = 0
k = l
while i < n1 and j < n2:
# No increment in inversion count
# if left[] has a smaller or equal element
if left[i] <= right[j]:
arr[k] = left[i]
i += 1
else:
arr[k] = right[j]
j += 1
res += (n1 - i)
k += 1
# Merge remaining elements
while i < n1:
arr[k] = left[i]
i += 1
k += 1
while j < n2:
arr[k] = right[j]
j += 1
k += 1
return res
Function to count inversions in the array
def countInv(arr, l, r): res = 0 if l < r: m = (r + l) // 2
# Recursively count inversions
# in the left and right halves
res += countInv(arr, l, m)
res += countInv(arr, m + 1, r)
# Count inversions such that greater element is in
# the left half and smaller in the right half
res += countAndMerge(arr, l, m, r)
return res
def inversionCount(arr): return countInv(arr, 0, len(arr) - 1)
if name == "main": arr = [4, 3, 2, 1] print(inversionCount(arr))
C#
// C# program to Count Inversions in an array using merge sort
using System; using System.Collections.Generic;
class GfG {
// This function merges two sorted subarrays arr[l..m] and arr[m+1..r]
// and also counts inversions in the whole subarray arr[l..r]
static int countAndMerge(int[] arr, int l, int m, int r) {
// Counts in two subarrays
int n1 = m - l + 1, n2 = r - m;
// Set up two arrays for left and right halves
int[] left = new int[n1];
int[] right = new int[n2];
for (int x = 0; x < n1; x++)
left[x] = arr[x + l];
for (int x = 0; x < n2; x++)
right[x] = arr[m + 1 + x];
// Initialize inversion count (or result)
// and merge two halves
int res = 0;
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
// No increment in inversion count
// if left[] has a smaller or equal element
if (left[i] <= right[j])
arr[k++] = left[i++];
else {
arr[k++] = right[j++];
res += (n1 - i);
}
}
// Merge remaining elements
while (i < n1)
arr[k++] = left[i++];
while (j < n2)
arr[k++] = right[j++];
return res;
}
// Function to count inversions in the array
static int countInv(int[] arr, int l, int r) {
int res = 0;
if (l < r) {
int m = (r + l) / 2;
// Recursively count inversions
// in the left and right halves
res += countInv(arr, l, m);
res += countInv(arr, m + 1, r);
// Count inversions such that greater element is in
// the left half and smaller in the right half
res += countAndMerge(arr, l, m, r);
}
return res;
}
static int inversionCount(int[] arr) {
return countInv(arr, 0, arr.Length - 1);
}
static void Main() {
int[] arr = {4, 3, 2, 1};
Console.WriteLine(inversionCount(arr));
}
}
JavaScript
// JavaScript program to Count Inversions in an array using merge sort
// This function merges two sorted subarrays arr[l..m] and arr[m+1..r] // and also counts inversions in the whole subarray arr[l..r] function countAndMerge(arr, l, m, r) {
// Counts in two subarrays
let n1 = m - l + 1, n2 = r - m;
// Set up two arrays for left and right halves
let left = arr.slice(l, m + 1);
let right = arr.slice(m + 1, r + 1);
// Initialize inversion count (or result)
// and merge two halves
let res = 0;
let i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
// No increment in inversion count
// if left[] has a smaller or equal element
if (left[i] <= right[j])
arr[k++] = left[i++];
else {
arr[k++] = right[j++];
res += (n1 - i);
}
}
// Merge remaining elements
while (i < n1)
arr[k++] = left[i++];
while (j < n2)
arr[k++] = right[j++];
return res;
}
// Function to count inversions in the array function countInv(arr, l, r) { let res = 0; if (l < r) { let m = Math.floor((r + l) / 2);
// Recursively count inversions
// in the left and right halves
res += countInv(arr, l, m);
res += countInv(arr, m + 1, r);
// Count inversions such that greater element is in
// the left half and smaller in the right half
res += countAndMerge(arr, l, m, r);
}
return res;
}
function inversionCount(arr) { return countInv(arr, 0, arr.length - 1); }
// Driver Code let arr = [4, 3, 2, 1]; console.log(inversionCount(arr));
`
**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.
**You may like to see: