How to Create a Mirror Image of A 2D Array in Java?17 Mar 2025 | 2 min read 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. Problem StatementWe 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. Mirror ImageIn 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 ProblemMirrorImage.java Output: ![]() |
Insertion sort is a simple sorting algorithm that puts together the final sorted array element by element. It loops through an array and extracts one component from the beginning data to insert it into the right place at the already sorted section of the array....
7 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
Linked List can be defined as a collection of objects called nodes that are randomly stored in the memory. A node contains two fields, i.e. data stored at that particular address and the pointer which contains the address of the node in the memory. The last...
2 min read
? Two strings are called anagrams if they contain same set of characters but in different order. "keep ? peek", "Mother In Law - Hitler Woman". File: AnagramString .java import java.util.Arrays; public class AnagramString { static void isAnagram(String str1, String str2) { String s1 = str1.replaceAll("\\s", ""); String s2 = str2.replaceAll("\\s", ""); boolean...
1 min read
In this section, we are going to solve another popular coding problem that was asked by e-commerce companies Flipkart and Amazon. Also, we will solve the problem with different approaches and create Java programs. Problem Statement In a boolean matrix of size m*n, we need to find if...
5 min read
In this program, we need to check whether the given matrix is an identity matrix. Identity Matrix A matrix is said to be an identity matrix if it is a square matrix in which elements of principle diagonal are ones, and the rest of the elements are...
4 min read
In this program, we need to find the transpose of the given matrix and print the resulting matrix. Transpose of Matrix Transpose of a matrix can be found by interchanging rows with the column that is, rows of the original matrix will become columns of the new...
4 min read
In this program, we need to check whether the given matrix is a sparse matrix. A matrix is said to be sparse matrix if most of the elements of that matrix are 0. It implies that it contains very less non-zero elements. To check whether the given...
3 min read
The natural numbers are the numbers that include all the positive integers from 1 to infinity. For example, 1, 2, 3, 4, 5, ......, n. When we add these numbers together, we get the sum of natural numbers. In this section, we will create the following programs: Java...
3 min read
In this program, we need to find the most repeated word present in given text file. This can be done by opening a file in read mode using file pointer. Read the file line by line. Split a line at a time and store in...
2 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