Count inversions in a permutation of first N natural numbers
Last Updated :
28 Jun, 2021
Given an array, arr[] of size N denoting a permutation of numbers from 1 to N, the task is to count the number of inversions in the array.
Note: Two array elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j.
Examples:
Input: arr[] = {2, 3, 1, 5, 4}
Output: 3
Explanation: Given array has 3 inversions: (2, 1), (3, 1), (5, 4).
Input: arr[] = {3, 1, 2}
Output: 2
Explanation: Given array has 2 inversions: (3, 1), (3, 2).
Different methods to solve inversion count has been discussed in the following articles:
Approach: This problem can be solved by using binary search. Follow the steps below to solve the problem:
- Store the numbers in the range [1, N] in increasing order in a vector, V.
- Initialize a variable, ans as 0 to store the number of inversions in the array, arr[].
- Iterate in the range [0, N-1] using the variable i
- Print the value of ans as the result.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int countInversions( int arr[], int n)
{
vector< int > v;
for ( int i = 1; i <= n; i++) {
v.push_back(i);
}
int ans = 0;
for ( int i = 0; i < n; i++) {
auto itr = lower_bound(
v.begin(), v.end(), arr[i]);
ans += itr - v.begin();
v.erase(itr);
}
cout << ans;
return 0;
}
int main()
{
int arr[] = { 2, 3, 1, 5, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
countInversions(arr, n);
return 0;
}
|
Java
import java.util.Vector;
class GFG{
static void countInversions( int arr[], int n)
{
Vector<Integer> v = new Vector<>();
for ( int i = 1 ; i <= n; i++)
{
v.add(i);
}
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
int itr = v.indexOf(arr[i]);
ans += itr;
v.remove(itr);
}
System.out.println(ans);
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 1 , 5 , 4 };
int n = arr.length;
countInversions(arr, n);
}
}
|
Python3
from bisect import bisect_left
def countInversions(arr, n):
v = []
for i in range ( 1 , n + 1 , 1 ):
v.append(i)
ans = 0
for i in range (n):
itr = bisect_left(v, arr[i])
ans + = itr
v = v[:itr] + v[itr + 1 :]
print (ans)
if __name__ = = '__main__' :
arr = [ 2 , 3 , 1 , 5 , 4 ]
n = len (arr)
countInversions(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void countInversions( int [] arr, int n)
{
List< int > v = new List< int >();
for ( int i = 1; i <= n; i++) {
v.Add(i);
}
int ans = 0;
for ( int i =0 ;i <n;i ++){
int itr = v.IndexOf(arr[i]);
ans += itr;
v.RemoveAt(itr);
}
Console.WriteLine(ans);
}
public static void Main( string [] args)
{
int [] arr = { 2, 3, 1, 5, 4 };
int n = arr.Length;
countInversions(arr, n);
}
}
|
Javascript
<script>
function countInversions(arr, n)
{
var v = [];
var i;
for (i = 1; i <= n; i++)
{
v.push(i);
}
var ans = 0;
for (i = 0; i < n; i++)
{
var index = v.indexOf(arr[i]);
ans += index;
v.splice(index, 1);
}
document.write(ans);
}
var arr = [ 2, 3, 1, 5, 4 ];
var n = arr.length;
countInversions(arr, n);
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...