Check if the given array is mirror inverse in Java
The primary objective is to check whether the given array is mirror inverse or not.
The values and position of the specified array are switched, and a duplicate array is created. The new arrays are then compared to the old array to see whether they are equal.
It is preferable to traverse the arrays and check each position to determine whether arr[arr[index]] = position is true. If it is, the collection is mirror inverse.
The aim is to determine if an array, arr[], is mirrored inverse. An array is said to be mirror-inverse only if the inverse is identical. The inverse of an array refers to swapping array components with their appropriate indices. Printing Yes or No depending on whether the arrays are mirror-inverse.
Array Mirror Inverse
If suppose you rewrite the collection contents, switch the column indices with matching array items. The supplied array is referred to as the mirror inverse if we discover that its inversion is equivalent. Let's use instances to grasp it better.
A Java program to determine whether an array is mirror-inverted or not
By switching the array's values and indices, another array is created.
Analyze the two arrays. The supplied array is a mirror inversion if the generated array matches the old arrays in size.
Example1: MirrorArray1.java
//this program is for checking whether the given array is MirrorInverse or not
//importing the required packages
import java.io.*;
import java.util.*;
// A class is created MirrorArray1
public class MirrorArray1
{
//that determines whether an array is mirror-inverse and returns true
static boolean isMirrorInverseArray(int array[])
{
for (int i = 0; i < array.length; i++)
{
// if the value meets the condition, then it returns true
// else it return False
if (array[array[i]] != i)
return false;
}
return true;
}
//main section of the program
public static void main(String args[])
{
// an array is initialized as a static array
int array[] = { 2, 1, 3, 0, 3};
// the function isMirrorInverseArray is called
if (isMirrorInverseArray(array))
// if the given array is mirror inverse
System.out.println("YES");
else
// if the given array is not the mirror inverse
System.out.println("NO");
}
}
Output
NO
Let's try another strategy.
Another method is to iterate through the provided array and check each index to see if the statement array[array[index]] = index is true. If it is, the provided array is mirror reverse. The method is superior to the one mentioned previously.
Example2: MirrorArray2.java
//this program is for checking whether the given array is MirrorInverse or not
//importing the required packages
import java.io.*;
import java.util.*;
// A class MirrorArray2 is created
public class MirrorArray2
{
// main section of the program
public static void main (String args[])
{
int c = 0;
// the given input array
int array[ ] = { 0,1,2,3};
// the for loop is used for iterating the given input array
for ( int i = 0 ; i < array.length ; i ++ )
{
if ( array [ array [ i ] ] == 1 )
{
//increases the c variable by 1 if such test mentioned above is successful.
c ++;
}
}
// the condition for printing the result
if (c!= 0)
{
System.out.println ( "The given input array is the mirror inverse array.") ;
}
else
{
System.out.println ("The given input array is not the mirror inverse array.");
}
}
}
Output:
The given input array is the mirror inverse array.