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

📅  最后修改于: 2021-10-26 05:12:54             🧑  作者: Mango

给定一个数组a[],在该数组上执行以下两种类型的反转:

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

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

例子:

方法:
要解决这个问题,需要了解两种反演的区别:

  • 对于类型 2 ,如果j = 5,那么i只能是4,因为j = i + 1
  • 对于类型 1,如果j = 5 ,则i可以从04 ,因为i小于j
  • 因此,类型 1的反演基本上是类型 2的反演与所有索引对 (i, j)相加其中i小于 ( j – 1 ) 并且a[i] > a[j]
  • 因此,对于任何索引j ,任务是检查是否存在索引i ,它小于 j – 1并且a[i] > a[j] 。如果找到任何这样的索引对(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


Javascript


输出:
Yes

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程