Check if the given array is mirror-inverse
Given an array arr[], the task is to find whether the array is mirror inverse. Inverse of an array means if the array elements are swapped with their corresponding indices and the array is called mirror-inverse if it’s inverse is equal to itself. If array is mirror-inverse then print Yes else print No.
Examples:
Input: arr[] = [3, 4, 2, 0, 1}
Output: Yes
In the given array:
index(0) -> value(3)
index(1) -> value(4)
index(2) -> value(2)
index(3) -> value(0)
index(4) -> value(1)
To find the inverse of the array, swap the index and the value of the array.
index(3) -> value(0)
index(4) -> value(1)
index(2) -> value(2)
index(0) -> value(3)
index(1) -> value(4)Inverse arr[] = {3, 4, 2, 0, 1}
So, the inverse array is equal to the given array.Input: arr[] = {1, 2, 3, 0}
Output: No
A simple approach is to create a new array by swapping the value and index of the given array and check whether the new array is equal to the original array or not.
A better approach is to traverse the array and for all the indices, if arr[arr[index]] = index is satisfied then the given array is mirror inverse.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include<bits/stdc++.h> using namespace std; // Function that returns true if // the array is mirror-inverse bool isMirrorInverse(int arr[], int n) { for (int i = 0; i < n; i++) { // If condition fails for any element if (arr[arr[i]] != i) return false; } // Given array is mirror-inverse return true; } // Driver code int main() { int arr[] = { 1, 2, 3, 0 }; int n = sizeof(arr)/sizeof(arr[0]); if (isMirrorInverse(arr,n)) cout << "Yes"; else cout << "No"; return 0; } // This code is contributed by Rajput-Ji |
Java
// Java implementation of the approach public class GFG { // Function that returns true if // the array is mirror-inverse static boolean isMirrorInverse(int arr[]) { for (int i = 0; i < arr.length; i++) { // If condition fails for any element if (arr[arr[i]] != i) return false; } // Given array is mirror-inverse return true; } // Driver code public static void main(String[] args) { int arr[] = { 1, 2, 3, 0 }; if (isMirrorInverse(arr)) System.out.println("Yes"); else System.out.println("No"); } } |
Python3
# Python 3 implementation of the approach # Function that returns true if # the array is mirror-inverse def isMirrorInverse(arr, n) : for i in range(n) : # If condition fails for any element if (arr[arr[i]] != i) : return False; # Given array is mirror-inverse return true; # Driver code if __name__ == "__main__" : arr = [ 1, 2, 3, 0 ]; n = len(arr) ; if (isMirrorInverse(arr,n)) : print("Yes"); else : print("No"); # This code is contributed by Ryuga |
C#
// C# implementation of the approach using System; class GFG { // Function that returns true if // the array is mirror-inverse static bool isMirrorInverse(int []arr) { for (int i = 0; i < arr.Length; i++) { // If condition fails for any element if (arr[arr[i]] != i) return false; } // Given array is mirror-inverse return true; } // Driver code static public void Main () { int []arr = { 1, 2, 3, 0 }; if (isMirrorInverse(arr)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by ajit... |
PHP
No
Recommended Posts:
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- Check if an array can be Arranged in Left or Right Positioned Array
- Check if an array is Wave Array
- Check whether an Array is Subarray of another Array
- Check whether a given array is a k sorted array or not
- Check if the array is beautiful
- Check whether the given array is perfect or not
- Check if it is possible to redistribute the Array
- Check if a given array is pairwise sorted or not
- Check if all elements of the array are palindrome or not
- Check if the given array contains all the divisors of some integer
- Check if the given number K is enough to reach the end of an array
- Check if array sum can be made K by three operations on it
- Check if array can be sorted with one swap
- Check if an array contains all elements of a given range
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.



