Open In App

Check if the given array is mirror-inverse

Improve
Improve
Like Article
Like
Save
Share
Report

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




<?php
// PHP implementation of the approach
 
// Function that returns true if
// the array is mirror-inverse
function 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.
?>


Javascript




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


Output: 

No

 

Time complexity: O(N), where N is the size of the given array.
Auxiliary space: O(1), no extra space is required.



Last Updated : 06 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads