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++
#include <iostream>
using namespace std;
void inverseEqual( int arr[], int n)
{
int brr[n];
for ( int i = 0; i < n; i++) {
int present_index = arr[i] - 1;
brr[present_index] = i + 1;
}
for ( int i = 0; i < n; i++) {
if (arr[i] != brr[i]) {
cout << "No" << endl;
return ;
}
}
cout << "Yes" << endl;
}
int main()
{
int n = 4;
int arr[n] = { 1, 4, 3, 2 };
inverseEqual(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void inverseEqual( int arr[], int n)
{
int [] brr = new int [n];
for ( int i = 0 ; i < n; i++)
{
int present_index = arr[i] - 1 ;
brr[present_index] = i + 1 ;
}
for ( int i = 0 ; i < n; i++)
{
if (arr[i] != brr[i])
{
System.out.println( "No" );
return ;
}
}
System.out.println( "Yes" );
}
public static void main(String[] args)
{
int n = 4 ;
int [] arr = { 1 , 4 , 3 , 2 };
inverseEqual(arr, n);
}
}
|
Python3
def inverseEqual(arr, n):
brr = [ 0 ] * n
for i in range (n):
present_index = arr[i] - 1
brr[present_index] = i + 1
for i in range (n):
if arr[i] ! = brr[i]:
print ( "NO" )
return
print ( "YES" )
n = 4
arr = [ 1 , 4 , 3 , 2 ]
inverseEqual(arr, n)
|
C#
using System;
class GFG{
static void inverseEqual( int []arr, int n)
{
int [] brr = new int [n];
for ( int i = 0; i < n; i++)
{
int present_index = arr[i] - 1;
brr[present_index] = i + 1;
}
for ( int i = 0; i < n; i++)
{
if (arr[i] != brr[i])
{
Console.WriteLine( "No" );
return ;
}
}
Console.WriteLine( "Yes" );
}
public static void Main(String[] args)
{
int n = 4;
int [] arr = { 1, 4, 3, 2 };
inverseEqual(arr, n);
}
}
|
Javascript
<script>
function inverseEqual(arr, n)
{
var brr = Array(n).fill(0);
for ( var i = 0; i < n; i++) {
var present_index = arr[i] - 1;
brr[present_index] = i + 1;
}
for ( var i = 0; i < n; i++) {
if (arr[i] != brr[i]) {
document.write( "No" );
return ;
}
}
document.write( "Yes" );
}
var n = 4;
var arr = [ 1, 4, 3, 2 ];
inverseEqual(arr, n);
</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++
#include <bits/stdc++.h>
using namespace std;
bool inverseEqual( int arr[], int n)
{
for ( int i = 0; i < n; i++)
if (arr[arr[i] - 1] != i + 1)
return false ;
return true ;
}
int main()
{
int n = 4;
int arr[n] = { 1, 4, 3, 2 };
cout << (inverseEqual(arr, n) ? "Yes" : "No" );
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean inverseEqual( int [] arr, int n){
for ( int i= 0 ;i<n;i++){
if (arr[arr[i] - 1 ] != i + 1 )
return false ;
}
return true ;
}
public static void main(String args[])
{
int n = 4 ;
int [] arr = { 1 , 4 , 3 , 2 };
System.out.println((inverseEqual(arr, n)== true )? "Yes" : "No" );
}
}
|
Python3
def inverseEqual(arr, n):
for i in range (n):
if (arr[arr[i] - 1 ] ! = i + 1 ):
return False
return True
n = 4
arr = [ 1 , 4 , 3 , 2 ]
print ( "Yes" if (inverseEqual(arr, n) = = True ) else "No" )
|
C#
using System;
class GFG
{
static bool inverseEqual( int [] arr, int n){
for ( int i=0;i<n;i++){
if (arr[arr[i] - 1] != i + 1)
return false ;
}
return true ;
}
public static void Main(String[] args)
{
int n = 4;
int [] arr = { 1, 4, 3, 2 };
Console.WriteLine((inverseEqual(arr, n)== true )? "Yes" : "No" );
}
}
|
Javascript
<script>
function inverseEqual(arr, n)
{
for (let i = 0; i < n; i++)
if (arr[arr[i] - 1] != i + 1)
return false ;
return true ;
}
let n = 4;
let arr = [ 1, 4, 3, 2 ];
document.write(inverseEqual(arr, n) ? "Yes" : "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
05 Sep, 2022
Like Article
Save Article