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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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...

chevron_right


PHP

Output:

No


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.





Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.