📌  相关文章
📜  检查数组上两个给定类型的求反计数是否相等

📅  最后修改于: 2021-05-17 23:44:01             🧑  作者: Mango

给定数组a [] ,将执行以下两种类型的求逆:

  • 使得A [i]> A [j]i 的索引对(i,j)的计数
  • 使得A [i]> A [j]j = i + 1的索引对(i,j)的计数

任务是检查两个反转的计数是否相等。如果它们相等,则打印“是” 。否则,打印“否”
例子:

方法:
要解决此问题,需要了解两个反演之间的区别:

  • 对于类型2 ,如果j = 5,只能是4,因为j = i + 1
  • 对于类型1,如果j = 5 ,则i可以从04 ,因为i小于j
  • 因此,1型的反转基本上总结了与所有对索引(I,J),其中i2型的反转小于(j – 1)和[Ⅰ]>一个[J]。
  • 因此,对于任何索引j ,任务是检查是否存在小于i – 1a [i]> a [j]的索引i 。如果找到任何这样的索引对(i,j) ,则打印“ No ”。否则,打印“”。

下面是上述方法的实现:

C++
// C++ Program to implement 
// the above approach 
  
#include  
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; 
}


Java
// 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


Python3
# 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


C#
// 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


输出:
Yes

时间复杂度: O(N)
辅助空间: O(1)