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 the 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 an 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to implement 
// the above approach 
  
#include <bits/stdc++.h> 
using namespace std; 
  
// Function to check if the 
// count of inversion of two 
// types are same or not 
bool solve(int a[], int n) 
    int mx = INT_MIN; 
  
    for (int j = 1; j < n; j++) { 
  
        // If maximum value is found 
        // to be greater than a[j], 
        // then that pair of indices 
        // (i, j) will add extra value 
        // to inversion of Type 1 
        if (mx > a[j]) 
  
            return false
  
        // Update max 
        mx = max(mx, a[j - 1]); 
    
  
    return true
  
// Driver code 
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; 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement 
// the above approach 
import java.io.*;
  
class GFG{
      
// Function to check if the 
// count of inversion of two 
// types are same or not 
static boolean solve(int a[], int n) 
    int mx = Integer.MIN_VALUE; 
  
    for(int j = 1; j < n; j++)
    {
          
        // If maximum value is found 
        // to be greater than a[j], 
        // then that pair of indices 
        // (i, j) will add extra value 
        // to inversion of Type 1 
        if (mx > a[j]) 
            return false
  
        // Update max 
        mx = Math.max(mx, a[j - 1]); 
    
    return true
}
      
// Driver code
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"); 
}
}
  
// This code is contributed by offbeat

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement  
# the above approach 
import sys
  
# Function to check if the 
# count of inversion of two 
# types are same or not 
def solve(a, n): 
      
    mx = -sys.maxsize - 1
  
    for j in range(1, n): 
  
        # If maximum value is found 
        # to be greater than a[j], 
        # then that pair of indices 
        # (i, j) will add extra value 
        # to inversion of Type 1 
        if (mx > a[j]): 
            return False
  
        # Update max 
        mx = max(mx, a[j - 1]) 
      
    return True
  
# Driver code 
a = [ 1, 0, 2
  
n = len(a) 
  
possible = solve(a, n) 
  
if (possible != 0):
    print("Yes"
else:
    print("No")
  
# This code is contributed by sanjoy_62

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement  
// the above approach 
using System; 
      
class GFG{ 
      
// Function to check if the 
// count of inversion of two 
// types are same or not 
static bool solve(int[] a, int n) 
    int mx = Int32.MinValue; 
      
    for(int j = 1; j < n; j++) 
    
              
        // If maximum value is found 
        // to be greater than a[j], 
        // then that pair of indices 
        // (i, j) will add extra value 
        // to inversion of Type 1 
        if (mx > a[j]) 
            return false
      
        // Update max 
        mx = Math.Max(mx, a[j - 1]); 
    
    return true
          
// Driver code 
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"); 
}
  
// This code is contributed by sanjoy_62

chevron_right


Output: 

Yes

Time Complexity: O(N) 
Auxiliary Space: O(1)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : offbeat, sanjoy_62