Check if the given two matrices are mirror images of one another in Java

In Java, the task of determining whether two matrices are mirror images of themselves or not is done by comparing the elements in equal positions of the matrices considering the reflection relation. By reversing the elements of a matrix horizontally, a mirror image is generated. Two matrices are referred to mirror images if the elements in corresponding positions of the one are equal to the reflected elements in the other.

Example 1:

Input

`mat1[][] = {{1, 2, 3}, {3, 4, 5}, {6, 7, 8}},mat2[][] = {{3, 2, 1}, {5, 4, 3}, {8, 7, 6}}`

Output

`Yes     `

Example 2:

Input

`mat1[][] = {{1, 4, 3}, {5, 4, 7}, {6, 7, 8}},mat2[][] = {{3, 4, 1}, {7, 4, 5}, {6, 7, 8}}`

Output

`No `

Approach 1: Matrix Reversal

The Matrix Reversal Approach is a method used to determine if two matrices are mirror images of each other. In the context of matrices, a mirror image is obtained by reversing the order of elements in each row. This approach involves reversing the elements of one of the matrices and then comparing it with the other matrix. If the reversed matrix is equal to the original matrix, the matrices are considered mirror images.

Algorithm

Step 1: A function named reverseArray is defined to reverse the elements of a one-dimensional array. The function takes an array arr as input and uses a two-pointer approach to swap elements from the start and end until the entire array is reversed. The reversed array is then returned.

Step 2: A function named reverseMatrix is defined to reverse the elements of each row in a given matrix. The function iterates through each row of the matrix and applies the reverseArray function to reverse the elements of that row.

Step 3: A function named areMatricesMirrorImages is defined to check if two matrices are mirror images of each other. The function takes two matrices, matrix1 and matrix2, as input.

Step 4: It checks if the dimensions of the matrices are the same. If not, the matrices cannot be mirror images, and the function returns false. The function then reverses the elements of matrix2 using the reverseMatrix function.

Step 5: Finally, it checks if the reversed matrix2 is equal to matrix1 using the Arrays.deepEquals method.

Step 6: In the main function, example matrices matrix1 and matrix2 are defined. The areMatricesMirrorImages function is called with these matrices.

Step 7: Depending on the result, a message is printed indicating whether the matrices are mirror images or not.

Filename: MatrixMirrorImage.java

`import java.util.Arrays;public class MatrixMirrorImage {    public static boolean areMatricesMirrorImages(int[][] matrix1, int[][] matrix2) {        int rows = matrix1.length;        int cols = matrix1[0].length;        // Check if dimensions are the same        if (rows != matrix2.length || cols != matrix2[0].length) {            return false;        }        // Reverse the elements of matrix2        for (int i = 0; i < rows; i++) {            matrix2[i] = reverseArray(matrix2[i]);        }        // Check if matrices are equal after reversal        return Arrays.deepEquals(matrix1, matrix2);    }    public static int[] reverseArray(int[] arr) {        int start = 0;        int end = arr.length - 1;        while (start < end) {            int temp = arr[start];            arr[start] = arr[end];            arr[end] = temp;            start++;            end--;        }        return arr;    }    public static void main(String[] args) {        // Example matrices        int[][] matrix1 = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };        int[][] matrix2 = { {3, 2, 1}, {6, 5, 4}, {9, 8, 7} };        // Check if matrices are mirror images        if (areMatricesMirrorImages(matrix1, matrix2)) {            System.out.println("The matrices are mirror images of each other.");        } else {            System.out.println("The matrices are not mirror images of each other.");        }    }}`

Output:

`The matrices are mirror images of each other.  `

Time Complexity

Reversing each row of a matrix takes O(cols) time, where cols is the number of columns. Reversing all rows takes O(rows * cols) time, where rows is the number of rows. Comparing two matrices using Arrays.deepEquals takes O(rows * cols) time. Therefore, the overall time complexity is O(rows * cols), where rows and cols are the dimensions of the matrices.

Space Complexity

Reversing each row in-place does not require additional space except for temporary variables. The overall space complexity is O(rows * cols), where rows is the number of rows and cols is the number of columns in the matrices and this includes the space required for the reversed matrix.

Approach 2: Naive Approach

The Naive Approach for checking if two matrices are mirror images of each other involves a straightforward and intuitive comparison of corresponding elements in the matrices. In this approach, each element in a row of the first matrix is compared with the mirrored element in the same row of the second matrix. If all corresponding elements satisfy the mirror image condition, the matrices are considered mirror images.

Algorithm

Step 1: Set rows as the number of rows and set cols as the number of columns in matrix1. Verify if the dimensions of matrix1 and matrix2 are the same. If not, return false as the matrices cannot be mirror images.

Step 2: Iterative Comparison: Iterate through each row (i) and each column (j) of the matrices using nested loops.

Step 3: Within the nested loop, check if the element at position [i][j] in matrix1 is equal to the mirrored element at position [i][cols - 1 - j] in matrix2. If any pair of elements does not satisfy this mirror image condition, return false.

Step 4: Mirror Image Condition Check: If all corresponding pairs of elements satisfy the mirror image condition, return true, indicating that the matrices are mirror images.

Step 5: In the main function, create example matrices matrix1 and matrix2. Call the areMatricesMirrorImages function with these matrices. Print a message indicating whether the matrices are mirror images or not.

Filename: MatrixMirrorImageN.java

`public class MatrixMirrorImageN {    public static boolean areMatricesMirrorImages(int[][] matrix1, int[][] matrix2) {        int rows = matrix1.length;        int cols = matrix1[0].length;        // Check if dimensions are the same        if (rows != matrix2.length || cols != matrix2[0].length) {            return false;        }        // Check if matrices are mirror images        for (int i = 0; i < rows; i++) {            for (int j = 0; j < cols; j++) {                // Check mirror image condition                if (matrix1[i][j] != matrix2[i][cols - 1 - j]) {                    return false;                }            }        }        return true;    }    public static void main(String[] args) {        // Example matrices        int[][] matrix1 = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };        int[][] matrix2 = { {3, 2, 1}, {6, 5, 4}, {9, 8, 7} };        // Check if matrices are mirror images        if (areMatricesMirrorImages(matrix1, matrix2)) {            System.out.println("The matrices are mirror images of each other.");        } else {            System.out.println("The matrices are not mirror images of each other.");        }    }}`

Output:

`The matrices are mirror images of each other.   `

Time Complexity

The time complexity of the Naive Approach is O(rows * cols), where rows is the number of rows and cols is the number of columns in the matrices. This is because the algorithm iterates through each element in the matrices once, performing a constant-time operation for each comparison.

Space Complexity

The space complexity of the Naive Approach is O(1), indicating constant space usage. The algorithm uses only a small amount of additional space for variables like rows, cols, i, and j. The space required is not dependent on the size of the input matrices, making it memory efficient.

Approach 3: Transposition and Comparison

The Transposition and Comparison Approach is a method used to determine if two matrices are mirror images of each other. It leverages the concept of matrix transposition, where the rows and columns of a matrix are swapped, and then compares the transposed matrix with the original matrix. If the transposed matrix is equal to the original matrix, they are considered mirror images.

Algorithm

Step 1: Set rows as the number of rows and set cols as the number of columns in matrix1. Verify if the dimensions of matrix1 and matrix2 are the same. If not, return false as the matrices cannot be mirror images.

Step 2: Matrix Transposition: Create a function transposeMatrix to transpose a given matrix and in the transposeMatrix function, create a new matrix transposed with dimensions [cols][rows].

Step 3: Iterate through each element in the original matrix, swapping the rows and columns during the transposition. Return the transposed matrix.

Step 4: Call the transposeMatrix function to transpose matrix2. Check if the transposed matrix is equal to matrix1 using the Arrays.deepEquals method.

Step 5: If they are equal, return true, indicating that the matrices are mirror images. Otherwise, return false.

Step 6: In the main function, create example matrices matrix1 and matrix2. Call the areMatricesMirrorImages function with these matrices. Print a message indicating whether the matrices are mirror images or not.

Filename: MatrixMirrorImageTC.java

`import java.util.Arrays;public class MatrixMirrorImageTC {    public static boolean areMatricesMirrorImages(int[][] matrix1, int[][] matrix2) {        int rows = matrix1.length;        int cols = matrix1[0].length;        // Check if dimensions are the same        if (rows != matrix2.length || cols != matrix2[0].length) {            return false;        }        // Transpose matrix2        int[][] transposedMatrix2 = transposeMatrix(matrix2);        // Check if matrices are equal after transposition        return Arrays.deepEquals(matrix1, transposedMatrix2);    }    public static int[][] transposeMatrix(int[][] matrix) {        int rows = matrix.length;        int cols = matrix[0].length;        int[][] transposed = new int[cols][rows];        for (int i = 0; i < rows; i++) {            for (int j = 0; j < cols; j++) {                transposed[j][i] = matrix[i][j];            }        }        return transposed;    }    public static void main(String[] args) {        // Example matrices        int[][] matrix1 = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };        int[][] matrix2 = { {3, 2, 1}, {6, 5, 4}, {9, 8, 7} };        // Check if matrices are mirror images        if (areMatricesMirrorImages(matrix1, matrix2)) {            System.out.println("The matrices are mirror images of each other.");        } else {            System.out.println("The matrices are not mirror images of each other.");        }    }}`

Output:

`The matrices are not mirror images of each other.    `

Time Complexity

The time complexity of the Transposition and Comparison Approach is O(rows * cols), where rows is the number of rows and cols is the number of columns in the matrices. The primary factor contributing to this complexity is the matrix transposition operation, which involves iterating through each element in the matrix once.

Space Complexity

The space complexity is O(rows * cols) due to the additional space needed for the transposed matrix. The transposed matrix is created as a new matrix with dimensions [cols][rows]. This space complexity is determined by the size of the matrices being processed.

Approach 4: Row Comparison with Reversed Rows

The Row Comparison with Reversed Rows Approach is a method used to determine if two matrices are mirror images of each other. This approach focuses on comparing each row of the first matrix with its reversed counterpart in the second matrix. If all corresponding pairs of rows satisfy the mirror image condition, the matrices are considered mirror images.

Algorithm

Step 1: Set rows as the number of rows and cols as the number of columns in matrix1. Verify if the dimensions of matrix1 and matrix2 are the same. If not, return false as the matrices cannot be mirror images.

Step 2: Iterate through each row (i) of the matrices using a loop. Reverse the row in matrix2 using the reverseArray function.

Step 3: Check if the reversed row is equal to the corresponding row in matrix1 using Arrays.equals. If any pair of rows does not satisfy the mirror image condition, return false.

Step 4: If all corresponding pairs of rows satisfy the mirror image condition, return true, indicating that the matrices are mirror images.

Step 5: Create a function reverseArray that reverses the order of elements in an array in-place. Initialize start as 0 and end as the last index of the array.

Step 6: While start is less than end, swap the elements at positions start and end and move the pointers accordingly.

Filename: MatrixMirrorImageRC.java

`import java.util.Arrays;public class MatrixMirrorImageRC {    public static boolean areMatricesMirrorImages(int[][] matrix1, int[][] matrix2) {        int rows = matrix1.length;        int cols = matrix1[0].length;        // Check if dimensions are the same        if (rows != matrix2.length || cols != matrix2[0].length) {            return false;        }        // Check if matrices are mirror images row-wise        for (int i = 0; i < rows; i++) {            // Reverse the row in matrix2            int[] reversedRow = reverseArray(matrix2[i]);            // Check if the reversed row is equal to the corresponding row in matrix1            if (!Arrays.equals(matrix1[i], reversedRow)) {                return false;            }        }        return true;    }    public static int[] reverseArray(int[] array) {        int start = 0;        int end = array.length - 1;        while (start < end) {            // Swap elements            int temp = array[start];            array[start] = array[end];            array[end] = temp;            // Move pointers            start++;            end--;        }        return array;    }    public static void main(String[] args) {        // Example matrices        int[][] matrix1 = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };        int[][] matrix2 = { {3, 2, 1}, {6, 5, 4}, {9, 8, 7} };        // Check if matrices are mirror images        if (areMatricesMirrorImages(matrix1, matrix2)) {            System.out.println("The matrices are mirror images of each other.");        } else {            System.out.println("The matrices are not mirror images of each other.");        }    }}`

Output:

`The matrices are mirror images of each other.`

Time Complexity

The time complexity of this approach is O(rows * cols), where rows is the number of rows and cols is the number of columns in the matrices. Each row is reversed in-place, and the comparison involves iterating through each element in the rows.

Space Complexity

The space complexity is O(1) as the algorithm uses only a constant amount of additional space for variables. The row reversal is performed in-place without requiring additional memory.