Check if the given array is mirror-inverse
Last Updated :
06 Sep, 2022
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++
#include<bits/stdc++.h>
using namespace std;
bool isMirrorInverse( int arr[], int n)
{
for ( int i = 0; i < n; i++)
{
if (arr[arr[i]] != i)
return false ;
}
return true ;
}
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;
}
|
Java
public class GFG {
static boolean isMirrorInverse( int arr[])
{
for ( int i = 0 ; i < arr.length; i++) {
if (arr[arr[i]] != i)
return false ;
}
return true ;
}
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
def isMirrorInverse(arr, n) :
for i in range (n) :
if (arr[arr[i]] ! = i) :
return False ;
return true;
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 0 ];
n = len (arr) ;
if (isMirrorInverse(arr,n)) :
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
static bool isMirrorInverse( int []arr)
{
for ( int i = 0; i < arr.Length; i++)
{
if (arr[arr[i]] != i)
return false ;
}
return true ;
}
static public void Main ()
{
int []arr = { 1, 2, 3, 0 };
if (isMirrorInverse(arr))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function isMirrorInverse( $arr )
{
for ( $i = 0; $i < sizeof( $arr ); $i ++)
{
if ( $arr [ $arr [ $i ]] != $i )
return false;
}
return true;
}
$arr = array (1, 2, 3, 0);
if (isMirrorInverse( $arr ))
echo ( "Yes" );
else
echo ( "No" );
?>
|
Javascript
<script>
function isMirrorInverse(arr) {
for (i = 0; i < arr.length; i++) {
if (arr[arr[i]] != i)
return false ;
}
return true ;
}
var arr = [ 1, 2, 3, 0 ];
if (isMirrorInverse(arr))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time complexity: O(N), where N is the size of the given array.
Auxiliary space: O(1), no extra space is required.
Share your thoughts in the comments
Please Login to comment...