Check if the given array is mirror-inverse

01 Apr, 2021
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++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include<bits/stdc++.h>using namespace std; // Function that returns true if// the array is mirror-inversebool 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 codeint 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 implementation of the approachpublic 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");    }}
# Python 3 implementation of the approach # Function that returns true if# the array is mirror-inversedef 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 codeif __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# implementation of the approachusing 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// PHP implementation of the approach // Function that returns true if// the array is mirror-inversefunction isMirrorInverse($arr){    for ($i = 0; $i < sizeof($arr); $i++)    {         // If condition fails for any element        if ($arr[$arr[$i]] != $i)            return false;    }     // Given array is mirror-inverse    return true;} // Driver code$arr = array(1, 2, 3, 0);if (isMirrorInverse($arr))    echo("Yes");else    echo("No"); // These code is contributed by Code_Mech.?>
<script> // JavaScript implementation of the approach      // Function that returns true if    // the array is mirror-inverse    function isMirrorInverse(arr) {        for (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             var arr = [ 1, 2, 3, 0 ];        if (isMirrorInverse(arr))            document.write("Yes");        else            document.write("No"); // This code contributed by Rajput-Ji </script>
No
 
jit_t
Rajput-Ji
ankthon
Code_Mech
Arrays
Java Programs
School Programming
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Stack Data Structure (Introduction and Program)
Linear Search
Initializing a List in Java
Java Programming Examples
Convert a String to Character Array in Java
Convert Double to Integer in Java
Implementing a Linked List in Java using Class