Check if the given array is same as its inverse permutation
Last Updated :
05 Sep, 2022
Given an array arr[] consisting of integers in the range [1, N], the task is to determine whether the Inverse Permutation of the given array is same as the given array.
An inverse permutation is a permutation obtained by inserting the position of all elements at the position equal to the respective values of the element in the array.
Illustration:
arr[] = {2, 4, 1, 3, 5}
The inverse permutation of the array will be equal to {3, 1, 4, 2, 5}
Examples:
Input: N = 4, arr[] = {1, 4, 3, 2}
Output: Yes
Explanation:
The inverse permutation of the given array is {1, 4, 3, 2} which is same as the given array.
Input: N = 5, arr[] = {2, 3, 4, 5, 1}
Output: No
Explanation:
The inverse permutation of the given array is {5, 1, 2, 3, 4} which is not the same as the given array.
Method 1 :
In this method, we will generate the inverse permutation of the array, then check if it is same as the original array.
Follow the steps below to solve the problem:
- Find the inverse permutation of the given array.
- Check, if the generated array is same as the original array.
- If both are same, then print Yes. Otherwise, print No.
Below is the implementation of the above approach:
C++
// C++ Program to implement
// the above approach
#include <iostream>
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
<script>
// Javascript Program to implement
// the above approach
// Function to check if the inverse
// permutation of the given array is
// same as the original array
function inverseEqual(arr, n)
{
// Stores the inverse
// permutation
var brr = Array(n).fill(0);
// Generate the inverse permutation
for (var i = 0; i < n; i++) {
var present_index = arr[i] - 1;
brr[present_index] = i + 1;
}
// Check if the inverse permutation
// is same as the given array
for (var i = 0; i < n; i++) {
if (arr[i] != brr[i]) {
document.write( "No" );
return;
}
}
document.write( "Yes" );
}
// Driver Code
var n = 4;
var arr = [ 1, 4, 3, 2 ];
inverseEqual(arr, n);
// This code is contributed by noob2000.
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 2 :
In this method, we take elements one by one and check for elements if at (arr[i] -1) index i+1 is present or not . If not then the inverse permutation of the given array is not the same as the array.
Below is the implementation of the above approach:
C++
// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
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
/*package whatever //do not write package name here */
import java.io.*;
class GFG
{
// Java program to implement
// the above approach
// Function to check if the inverse
// permutation of the given array is
// same as the original array
static boolean 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
public static void main(String args[])
{
int n = 4;
int[] arr = { 1, 4, 3, 2 };
// Function Call
System.out.println((inverseEqual(arr, n)==true)?"Yes" : "No");
}
}
// This Code is contributed by shinjanpatra
Python3
# Python 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):
# Check the if inverse permutation
# is not same
for i in range(n):
if (arr[arr[i] - 1] != i + 1):
return False
return True
# Driver Code
n = 4
arr = [ 1, 4, 3, 2 ]
# Function Call
print("Yes" if (inverseEqual(arr, n)==True) else "No")
# This code is contributed by shinjanpatra
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 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
public static void Main(String[] args)
{
int n = 4;
int[] arr = { 1, 4, 3, 2 };
// Function Call
Console.WriteLine((inverseEqual(arr, n)==true)?"Yes" : "No");
}
}
// This Code is contributed by Pushpesh Raj.
JavaScript
<script>
// Javascript program to implement
// the above approach
// Function to check if the inverse
// permutation of the given array is
// same as the original array
function inverseEqual(arr, n)
{
// Check the if inverse permutation
// is not same
for (let i = 0; i < n; i++)
if (arr[arr[i] - 1] != i + 1)
return false;
return true;
}
// Driver Code
let n = 4;
let arr = [ 1, 4, 3, 2 ];
// Function Call
document.write(inverseEqual(arr, n) ? "Yes" : "No");
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Check if Array elements of given range form a permutation Given an array arr[] consisting of N distinct integers and an array Q[][2] consisting of M queries of the form [L, R], the task for each query is to check if array elements over the range [L, R] forms a permutation or not. Note: A permutation is a sequence of length N containing each number from 1 t
13 min read
Find permutation array from the cumulative sum array Given an array arr[] of N elements where each arr[i] is the cumulative sum of the subarray P[0...i] of another array P[] where P is the permutation of the integers from 1 to N. The task is to find the array P[], if no such P exists then print -1.Examples: Input: arr[] = {2, 3, 6} Output: 2 1 3Input:
5 min read
Check if two arrays are permutations of each other Given two unsorted arrays of the same size, write a function that returns true if two arrays are permutations of each other, otherwise false. Examples: Input: arr1[] = {2, 1, 3, 5, 4, 3, 2} arr2[] = {3, 2, 2, 4, 5, 3, 1}Output: YesInput: arr1[] = {2, 1, 3, 5,} arr2[] = {3, 2, 2, 4}Output: NoWe stron
15+ min read
Check if an Array is a permutation of numbers from 1 to N : Set 2 Given an array arr containing N positive integers, the task is to check if the given array arr represents a permutation or not. A sequence of N integers is called a permutation if it contains all integers from 1 to N exactly once. Examples: Input: arr[] = {1, 2, 5, 3, 2} Output: No Explanation: The
4 min read
Longest permutation subsequence in a given array Given an array arr containing N elements, find the length of the longest subsequence such that it is a valid permutation of a particular length. If no such permutation sequence exists then print 0.Examples: Input: arr[] = {3, 2, 1, 6, 5} Output: 3 Explanation: Longest permutation subsequence will be
5 min read