×

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.

Related Topics

Deadlock in Java

Deadlock is when two or more processes wait for the state to do their tasks, but none of them can do so. It is a very common problem that one...

6 minutes read.

Java class class

Java Class class instances are an executing Java application's implementation of the classes and interfaces. As well as, every Array is indeed an object that is common for all Arrays...

6 minutes read.

Java StringWriter Class

The StringWriter class is a character stream in which it is used to store the output consisting of characters into the string buffer. Upon collecting output into a string buffer,...

3 minutes read.

Level order Traversal of a Binary Tree in Java

In Java, a level order traversal of a tree structure is also referred to as the breadth-first traversal of the binary tree. Regarding the subsequent binary tree: Level order traversal is as...

5 minutes read.

Java Finally Keyword

The final block in Java is used to run essential code, such as connection closure, among other things. Whether an exception is resolved or not, the Java finally block has...

3 minutes read.

Why we use static in Java

Many reserved keywords in Java cannot be used as variable names or identifiers. Java uses static keywords frequently because of its effective memory management system. In most cases, you must...

3 minutes read.

Difference between C, C++, java

C Language: C language is a procedure oriented language. It has been invented by Dennis Ritchie in the year 1970. It is one of the computer programming language. The purpose of...

3 minutes read.

Java Try-Catch Block

Java Try Block The handling of the exceptions in a block of code is done with the help of java try block. It throws the code that is enclosed in a...

3 minutes read.

Session Tracking in Java

When a series of requests from the same User (i.e., requests coming from the same browser) occurs over an extended period of time, servlets employ a mechanism known as session...

3 minutes read.

Java Read File

Java provides its users with many ways of reading a file. To read a text file, you can use a FileReader, BufferedReader, or Scanner. Each utility offers something special for...

6 minutes read.

Java Constant

A constant is an unchangeable entity in coding, as its title implies. The value which cannot be altered, in other terms. We shall understand about Java constants and exactly how...

3 minutes read.

Logger class in Java

Logging is a crucial component of Java that aids developers in tracking down mistakes. The logging technique is included with the computer language Java. The possibility of collect the log...

7 minutes read.

Java Boolean Class

The Boolean class wraps a value of the Boolean primitive in an object. Its object contains only a single field whose type is Boolean. Boolean Methods: This class contains several different methods...

2 minutes read.

Program to check whether a given character is present in a string or not

In this article, you will understand the logic to find out whether the given character is present in the string or not and find out the position of the specified...

3 minutes read.

How to create a mirror image of a 2D array in Java

Problem Statement We have provided a list of m x n. here m indicates rows, and n indicates columns). Printing the fresh matrices should result in a mirror reflection of an...

2 minutes read.

System Class in Java

The System class provides features including a way to load files and libraries, standard input, standard output, and errors throughput streams, accessibility to outside defined characteristics and environment variables, and...

3 minutes read.

Beautiful Array in Java

In this section, we will be acknowledged about the beautiful array in Java, its characteristics, how it is achieved. What are the types of approaches and what are the tools...

8 minutes read.

Java Math tanh() Method

The tanh() method of Java Math class returns the hyperbolic tangent of the specified double value. Syntax: public static double tanh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic tangent is to...

2 minutes read.

How to Update Java

As we all know that java can be installed in all operating systems like windows, Linux, macOS. We are available with the java 17 and java 18 versions in the...

3 minutes read.

Web Service Response Time Calculation in Java

Administration response time or reaction time is the typical measure of time it takes for a solicitation to be handled by a PC framework, like your organization switch. In the...

4 minutes read.