×

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

Java String getBytes() method

getBytes() method returns sequence of bytes. Syntax: There are three variants of getBytes() method: public byte[] getBytes()        public byte[] getBytes(String charsetName) public byte[] getBytes(Charset charset) Returns: It returns sequence of bytes. Java String getBytes() Example...

2 minutes read.

Array Programs in Java

Array Programs in Java: An array is a data structure that stores similar elements in a contiguous memory location. In Java, an array is an object that stores the same...

7 minutes read.

List all files in a Directory in Java

The list of all files in the directory can be done using Java. You should be aware that a directory may include a subfolder and that subdirectory may also contain some...

3 minutes read.

Group by in Java 8

By using this groupingBy() method, developers can directly able to perform the "GROUP BY" operation. The thing is, now, the Java 8 programming language allows the programmers to do this...

3 minutes read.

Minimum Lights to Activate Java Snippet Class

Minimum Lights to Activate Problem in Java In prison, there is a hallway that is N units long. Given an N-dimensional array A. If the light at the ith position is...

3 minutes read.

Generic queue in Java

Before understanding how to implement a generic queue in java, one must know about generics and queue in java. Generics in Java Generics are parameterized types. The goal is to enable type...

6 minutes read.

Types of JDBC Drivers

JDBC Drivers: A piece of software known as JDBC Driver permits database communication between Java applications and the server. In order to communicate with our database server, JDBC drivers put into practice...

4 minutes read.

Difference Between Access Specifiers and Modifiers in Java

Java employs access modifiers to restrict a class's data members, member functions, and constructor. Access modifiers are essential when creating Java program and applications. Access modifiers in Java include: defaultpublicprotectedprivate Default Access Modifiers Without...

4 minutes read.

Split the Number String into Primes in Java

Given is a string that only contains digits and serves to represent a number. Our goal is to split the string of numbers in a way that ensures each segment...

2 minutes read.

Difference Between Data Hiding and Abstraction in Java

Abstraction: Data Abstraction and Data Hiding ideas are utilised to show the expected data to the end client and conceal the superfluous subtleties, however, for specific purposes like decreasing the framework's...

10 minutes read.

Exception Handling Program in Java

Exception Handling Program in Java Exception means something that is abnormal. In Java, an exception is treated as a problem that disrupts the normal flow of the program. An exception leads...

7 minutes read.

java.lang.NumberFormatException for Input String

java.lang.NumberFormatException for Input String The exception java.lang.NumberFormatException for input string occurs when we try to convert a string into a number format. For example, if someone converts the string “Tutorial & Example”...

3 minutes read.

Java Integer signum() method

The signum() method of Java Integer class returns the signum function of the specified int value. Syntax public static int signum (int i)  Parameters The parameter ‘i’ represents the value whose signum is to...

1 minute read.

Java Flags Enum

In a programming language, enumerations represent a group of named constants.For instance; the four suits in a deck of playing cards could be the enumerators Club, Diamond, Heart, and Spade,...

3 minutes read.

Java Math ceil() Method

The ceil() method of Math class returns the smallest double value which is closest to negative infinity and is greater than or equal to the argument. Syntax: public static double ceil(double a) Parameters: The...

2 minutes read.

Libraries in Java

The Java Class Library (JCL) specifically is a set of dynamically loadable libraries that Java Virtual Machine (JVM) languages can call at any run time, which is fairly significant because...

6 minutes read.

Special Operators in Java

An operator in Java is a special symbol. It is used to perform operations on two or more variables.  We all know basic operations in Java. There are 8 types...

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

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.

Arithmetic Operations on String in Java

Introduction Arithmetic, Relational, Bitwise, and Logical operators are all available in Java. Simple mathematical calculations are performed using Java arithmetic operators. Basic Arithmetic operators are considered in Java to be Addition,...

4 minutes read.