How to Create a Mirror Image of A 2D Array in Java?
It is a very interesting problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, IBM 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 discuss how to create a mirror image of a 2D array in Java with different approaches and logic. Also, we will create Java programs for the same.
We have given an array of m x n (where m is the number of rows and n is the number of columns). Print the new matrix in such a way that the new matrix is the mirror image of the original matrix.
In a mirror image of a 2D array of m*n, elements of the first and the last columns are interchanged. The middle column remains fixed, as shown in the following figure. Note that only columns of a mirror image are interchanged row remains unaffected.
If the matrix is of 4*4, the second and fourth columns will also be interchanged.
Note: If in the given matrix the number of columns is odd the middle column will be fixed and the rest of the columns will be interchanged.
Solution to the Problem