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 permutationis 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:YesExplanation:

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:NoExplanation:

The inverse permutation of the given array is {5, 1, 2, 3, 4} which is not the same as the given array.

**Approach :**

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; ` `} ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

**Output:**

Yes

**Time Complexity:** O(N)**Auxiliary Space:** O(N)

## Recommended Posts:

- Inverse Permutation
- Check if the given array is mirror-inverse
- Decimal Equivalent of Gray Code and its Inverse
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Check whether two strings contain same characters in same order
- Check if given permutation of 1 to N is feasible using given operations
- Check if the given permutation is a valid BFS of a given Tree
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Count Derangements (Permutation such that no element appears in its original position)
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Finding 'k' such that its modulus with each array element is same
- Minimize elements to be added to a given array such that it contains another given array as its subsequence
- Find Array formed by adding each element of given array with largest element in new array to its left
- Check if its possible to make sum of the array odd with given Operations
- Check if the given permutation is a valid DFS of graph
- Check if there exists a permutation of given string which doesn't contain any monotonous substring
- Check if given permutation of 1 to N can be counted in clockwise or anticlockwise direction

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.