📌  相关文章
📜  检查给定数组是否与其逆排列相同

📅  最后修改于: 2021-06-25 14:00:54             🧑  作者: Mango

给定数组arr [][1,N]范围内的整数组成,任务是确定给定数组的逆排列是否与给定数组相同。

例子:

方法1:

在这种方法中,我们将生成数组的逆排列,然后检查它是否与原始数组相同。

请按照以下步骤解决问题:

  • 查找给定数组的逆排列
  • 检查生成的数组是否与原始数组相同。
  • 如果两者相同,则打印“是” 。否则,打印No。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to check if the inverse
// permutation of the given array is
// same as the original array
void inverseEqual(int arr[], int n)
{
 
    // Stores the inverse
    // permutation
    int brr[n];
 
    // Generate the inverse permutation
    for (int i = 0; i < n; i++) {
        int present_index = arr[i] - 1;
        brr[present_index] = i + 1;
    }
 
    // Check if the inverse permutation
    // is same as the given array
    for (int i = 0; i < n; i++) {
        if (arr[i] != brr[i]) {
            cout << "No" << endl;
            return;
        }
    }
 
    cout << "Yes" << endl;
}
 
// Driver Code
int main()
{
 
    int n = 4;
    int arr[n] = { 1, 4, 3, 2 };
 
    inverseEqual(arr, n);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to check if the inverse
// permutation of the given array is
// same as the original array
static void inverseEqual(int arr[], int n)
{
     
    // Stores the inverse
    // permutation
    int[] brr = new int[n];
 
    // Generate the inverse permutation
    for(int i = 0; i < n; i++)
    {
        int present_index = arr[i] - 1;
        brr[present_index] = i + 1;
    }
 
    // Check if the inverse permutation
    // is same as the given array
    for(int i = 0; i < n; i++)
    {
        if (arr[i] != brr[i])
        {
            System.out.println("No");
            return;
        }
    }
    System.out.println("Yes");
}
 
// Driver code
public static void main(String[] args)
{
    int n = 4;
    int[] arr = { 1, 4, 3, 2 };
 
    inverseEqual(arr, n);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to implement
# the above approach
 
# Function to check if the inverse
# permutation of the given array is
# same as the original array
def inverseEqual(arr, n):
     
    # Stores the inverse
    # permutation
    brr = [0] * n
     
    # Generate the inverse permutation
    for i in range(n):
        present_index = arr[i] - 1
        brr[present_index] = i + 1
         
    # Check if the inverse permutation
    # is same as the given array
    for i in range(n):
        if arr[i] != brr[i]:
            print("NO")
            return
             
    print("YES")
     
# Driver code
n = 4
arr = [ 1, 4, 3, 2 ]
 
inverseEqual(arr, n)
 
# This code is contributed by Stuti Pathak


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to check if the inverse
// permutation of the given array is
// same as the original array
static void inverseEqual(int []arr, int n)
{
     
    // Stores the inverse
    // permutation
    int[] brr = new int[n];
 
    // Generate the inverse permutation
    for(int i = 0; i < n; i++)
    {
        int present_index = arr[i] - 1;
        brr[present_index] = i + 1;
    }
 
    // Check if the inverse permutation
    // is same as the given array
    for(int i = 0; i < n; i++)
    {
        if (arr[i] != brr[i])
        {
            Console.WriteLine("No");
            return;
        }
    }
    Console.WriteLine("Yes");
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 4;
    int[] arr = { 1, 4, 3, 2 };
 
    inverseEqual(arr, n);
}
}
 
// This code is contributed by sapnasingh4991


Javascript


C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to check if the inverse
// permutation of the given array is
// same as the original array
bool inverseEqual(int arr[], int n)
{
 
    // Check the if inverse permutation is not same
    for (int i = 0; i < n; i++)
        if (arr[arr[i] - 1] != i + 1)
            return false;
 
    return true;
}
 
// Driver Code
int main()
{
    int n = 4;
    int arr[n] = { 1, 4, 3, 2 };
 
    // Function Call
    cout << (inverseEqual(arr, n) ? "Yes" : "No");
 
    return 0;
}


Javascript


输出
Yes

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

方法2:

在这种方法中,我们一个接一个地处理元素,并检查是否存在(arr [i] -1)索引i + 1的元素。如果不是,则给定数组的反渗透与数组不同。

下面是上述方法的实现:

C++

// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to check if the inverse
// permutation of the given array is
// same as the original array
bool inverseEqual(int arr[], int n)
{
 
    // Check the if inverse permutation is not same
    for (int i = 0; i < n; i++)
        if (arr[arr[i] - 1] != i + 1)
            return false;
 
    return true;
}
 
// Driver Code
int main()
{
    int n = 4;
    int arr[n] = { 1, 4, 3, 2 };
 
    // Function Call
    cout << (inverseEqual(arr, n) ? "Yes" : "No");
 
    return 0;
}

Java脚本


输出
Yes

时间复杂度: O(N)

辅助空间: O(1)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。