Check If the Given Array is Mirror Inverse in Java17 Mar 2025 | 3 min read It is a very interesting problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to check if the given array is mirror inverse or not in Java with different approaches and logic. Also, we will create Java programs for the same. Problem StatementWe have given an array named a[], the task is to find whether the array is mirror inverse or not. If an array is mirror-inverse then print Yes else print No. Reverse of ArrayThe reverse of an array can be found by writing the array right to left. Example: ![]() Mirror Inverse of ArrayIf we swap array indices with corresponding array elements and rewrite the array values. If we get its inverse is equal to the given array, the array is called the mirror inverse. Let's understand it through examples. Note: Reverse and mirror inverse array are not the same.Example 1:Input: arr[] = [3, 4, 2, 0, 1, 5, 6} Output: Yes In the given array: index(0) -> value(3) index(1) -> value(4) index(2) -> value(2) index(3) -> value(0) index(4) -> value(1) index(5) -> value(5) index(6) -> value(6) In order to find the inverse of the array, swap the index and the value of the given array. Hence, we get: index(3) -> value(0) index(4) -> value(1) index(2) -> value(2) index(0) -> value(3) index(1) -> value(4) index(5) -> value(5) index(6) -> value(6) Inverse arr[] = {3, 4, 2, 0, 1, 5, 6} We observe that the inverse array is equal to the given array. So, it is a mirror inverse of the given array. Example 2:Input: arr[] = {1, 3, 5, 7, 9} index(0) -> value(1) index(1) -> value(3) index(2) -> value(5) index(3) -> value(7) index(4) -> value(9) In order to find the inverse of the array, swap the index and the value of the given array. Hence, we get: index(1) -> value(0) index(3) -> value(1) index(5) -> value(2) index(7) -> value(3) index(9) -> value(4) Inverse arr[] = {0, 1, 2, 3, 4} We observe that the inverse array is not equal to the given array. So, it is not a mirror inverse of the given array. Java Program to Check if the Given Array is a Mirror Inverse or Not
MirrorInverseArray1.java Output: No Let's see another approach. Another approach is that traverse over the given array and for all the indices if the condition array[array[index]] = index satisfies then the given array is mirror inverse. The approach is better than the above approach. MirrorInverseArray2.java Output: The given array is mirror inverse. |
In this program, we need to count the number of characters present in the string: The best of both worlds To count the number of characters present in the string, we will iterate through the string and count the characters. In above example, total numbers of characters...
2 min read
In this program, we need to get the result of subtraction of two matrices. Two matrices A and B can be subtracted if and only if they have same dimensions that are, the same number of rows and columns. It is not possible to subtract a...
4 min read
In this program, we will create a circular linked list and delete a node from the middle of the list. If the list is empty, display the message "List is empty". If the list is not empty, we will calculate the size of the list...
9 min read
In this program, we will create a circular linked list and insert every new node at the end of the list. If the list is empty, then head and tail will point to the newly added node. If the list is not empty, the newly...
6 min read
Java Convert Decimal to Binary We can convert decimal to binary in java using Integer.toBinaryString() method or custom logic. Java Decimal to Binary conversion: Integer.toBinaryString() The Integer.toBinaryString() method converts decimal to binary string. The signature of toBinaryString() method is given below: public static String toBinaryString(int decimal) Let's see the simple example...
1 min read
Java Program to Print Spiral Pattern The spiral pattern (or matrix in spiral form) is frequently asked in Java interviews and academics. In this section, we will create a Java program to create a spiral pattern or spiral matrix. What is Spiral Matrix or Spiral Pattern? A spiral...
7 min read
In this program, we will create a singly linked list and delete a node from the end of the list. To accomplish this task, we first find out the second last node of the list. Then, make second last node as the new tail of...
6 min read
Java Convert String to Object We can convert String to Object in java with assignment operator. Each class is internally a child class of Object class. So you can assign string to Object directly. You can also convert String to Class type object using Class.forName() method. Java String to...
1 min read
In this program, we need to multiply two matrices and print the resulting matrix. Product of two matrices The product of two matrices can be computed by multiplying elements of the first row of the first matrix with the first column of the second matrix then, add...
6 min read
Java Convert String to boolean We can convert String to boolean in java using Boolean.parseBoolean(string) method. To convert String into Boolean object, we can use Boolean.valueOf(string) method which returns instance of Boolean class. To get boolean true, string must contain "true". Here, case is ignored. So, "true" or "TRUE"...
1 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India