Check if the count of inversions of two given types on an Array are equal or not
Given an array a[] on which, the following two types of inversions are performed:
- Count of pairs of indices (i, j) such that A[i] > A[j] and i < j
- Count of pairs of indices (i, j) such that A[i] > A[j] and j = i + 1
The task is to check if the count of both the inversions is equal or not. If they are equal, print “Yes”. Otherwise, print “No”.
Examples:
Input: a[] = {1, 0, 2}
Output: Yes
Explanation:
Count of inversion of Type 1 = 1 [(i, j) : (0, 1)]
Count of inversion of Type 2 = 1 [(i, j) : (0, 1)]
Input: a[] = {1, 2, 0}
Output: No
Explanation:
Count of inversion of Type 1 = 2 [(i, j) : (0, 2);(1, 2)]
Count of inversion of Type 2 = 1 [(i, j) : (1, 2)]
Approach:
To solve the problem, the difference between the two inversions need to be understood:
- For Type 2, if j = 5, then i can only be 4 as j = i + 1
- For Type 1, if j = 5, then i can be from 0 to 4, as i is less than j.
- Therefore, the inversion of Type 1 is basically an inversion of Type 2 summed up with all pair of indices(i, j) with i which is less than (j – 1) and a[i] > a[j].
- So, for any index j, the task is to check if there is index i, which is less than j – 1 and a[i] > a[j]. If any such pair of indices (i, j) is found, then print “No“. Otherwise, print “Yes“.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool solve( int a[], int n)
{
int mx = INT_MIN;
for ( int j = 1; j < n; j++) {
if (mx > a[j])
return false ;
mx = max(mx, a[j - 1]);
}
return true ;
}
int main()
{
int a[] = { 1, 0, 2 };
int n = sizeof (a) / sizeof (a[0]);
bool possible = solve(a, n);
if (possible)
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
import java.io.*;
class GFG{
static boolean solve( int a[], int n)
{
int mx = Integer.MIN_VALUE;
for ( int j = 1 ; j < n; j++)
{
if (mx > a[j])
return false ;
mx = Math.max(mx, a[j - 1 ]);
}
return true ;
}
public static void main (String[] args)
{
int a[] = { 1 , 0 , 2 };
int n = a.length;
boolean possible = solve(a, n);
if (possible)
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
import sys
def solve(a, n):
mx = - sys.maxsize - 1
for j in range ( 1 , n):
if (mx > a[j]):
return False
mx = max (mx, a[j - 1 ])
return True
a = [ 1 , 0 , 2 ]
n = len (a)
possible = solve(a, n)
if (possible ! = 0 ):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool solve( int [] a, int n)
{
int mx = Int32.MinValue;
for ( int j = 1; j < n; j++)
{
if (mx > a[j])
return false ;
mx = Math.Max(mx, a[j - 1]);
}
return true ;
}
public static void Main ()
{
int [] a = { 1, 0, 2 };
int n = a.Length;
bool possible = solve(a, n);
if (possible)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function solve(a, n)
{
let mx = 0;
for (let j = 1; j < n; j++)
{
if (mx > a[j])
return false ;
mx = Math.max(mx, a[j - 1]);
}
return true ;
}
let a = [ 1, 0, 2 ];
let n = a.length;
let possible = solve(a, n);
if (possible)
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
13 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...