Check if the given two matrices are mirror images of one another - GeeksforGeeks

23 Apr, 2021
Given two matrices mat1[][] and mat2[][] of size NxN. The task is to find if the given two matrices are mirror images of one another. Print “Yes” if the given two matrices are mirror images, otherwise print “No”.
Two matrices mat1 and mat2 of size N*N are said to be mirror images of one another if for any valid index (i, j) of the matrix:  
mat1[i][j] = mat2[i][N-j-1]
 
Examples:  
Input: mat1[][] = {{1, 2, 3}, {3, 4, 5}, {6, 7, 8}}, mat2[][] = {{3, 2, 1}, {5, 4, 3}, {8, 7, 6}} Output: Yes  
Input: mat1 = {{1, 2, 3}, {5, 4, 1}, {6, 7, 2}}; mat2 = {{3, 2, 1}, {5, 4, 1}, {2, 7, 6}}; Output: No  
Approach: The approach is based on the observation that a matrix is a mirror image of another only if the elements of each row of the first matrix are equal to the reversed row of elements of the other matrix. Follow the steps below :  
Traverse over the matrix mat1[][] row-wise from start to end and over mat2[][] row-wise from end to start.
While traversing, if any element of mat1[][] is found to be not equal to the element at mat2[][], then print “No”
After traversing both the matrices, if all the elements are found to be equal, print “Yes”.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of// the above approach#include <bits/stdc++.h>using namespace std; // Function to check whether the// two matrices are mirror// of each othervoid mirrorMatrix(int mat1[][4],                  int mat2[][4], int N){    // Initialising row and column of    // second matrix    int row = 0;    int col = 0;     bool isMirrorImage = true;     // Iterating over the matrices    for (int i = 0; i < N; i++) {         // Check row of first matrix with        // reversed row of second matrix        for (int j = N - 1; j >= 0; j--) {             // If the element is not equal            if (mat2[row][col] != mat1[i][j]) {                isMirrorImage = false;            }             // Increment column            col++;        }         // Reset column to 0        // for new row        col = 0;         // Increment row        row++;    }     if (isMirrorImage)        cout << "Yes";    else        cout << "No";} // Driver codeint main(){    // Given 2 matrices    int N = 4;    int mat1[][4] = { { 1, 2, 3, 4 },                      { 0, 6, 7, 8 },                      { 9, 10, 11, 12 },                      { 13, 14, 15, 16 } };     int mat2[][4] = { { 4, 3, 2, 1 },                      { 8, 7, 6, 0 },                      { 12, 11, 10, 9 },                      { 16, 15, 14, 13 } };     // Function Call    mirrorMatrix(mat1, mat2, N);}
// Java implementation of// the above approachimport java.util.*;class GFG{ // Function to check whether the// two matrices are mirror// of each otherstatic void mirrorMatrix(int mat1[][],                         int mat2[][], int N){  // Initialising row and column of  // second matrix  int row = 0;  int col = 0;   boolean isMirrorImage = true;   // Iterating over the matrices  for (int i = 0; i < N; i++)  {    // Check row of first matrix with    // reversed row of second matrix    for (int j = N - 1; j >= 0; j--)    {      // If the element is not equal      if (mat2[row][col] != mat1[i][j])      {        isMirrorImage = false;      }       // Increment column      col++;    }     // Reset column to 0    // for new row    col = 0;     // Increment row    row++;  }   if (isMirrorImage)    System.out.print("Yes");  else    System.out.print("No");} // Driver codepublic static void main(String[] args){  // Given 2 matrices  int N = 4;  int mat1[][] = {{1, 2, 3, 4},                  {0, 6, 7, 8},                  {9, 10, 11, 12},                  {13, 14, 15, 16}};   int mat2[][] = {{4, 3, 2, 1},                  {8, 7, 6, 0},                  {12, 11, 10, 9},                  {16, 15, 14, 13}};   // Function Call  mirrorMatrix(mat1, mat2, N);}} // This code is contributed by 29AjayKumar
# Python3 implementation of# the above approach # Function to check whether the# two matrices are mirror# of each otherdef mirrorMatrix(mat1, mat2, N):     # Initialising row and column of    # second matrix    row = 0    col = 0     isMirrorImage = True     # Iterating over the matrices    for i in range(N):         # Check row of first matrix with        # reversed row of second matrix        for j in range(N - 1, -1, -1):             # If the element is not equal            if (mat2[row][col] != mat1[i][j]):                isMirrorImage = False             # Increment column            col += 1         # Reset column to 0        # for new row        col = 0         # Increment row        row += 1     if (isMirrorImage):        print("Yes")    else:        print("No") # Driver codeif __name__ == '__main__':         # Given 2 matrices    N = 4    mat1 = [ [ 1, 2, 3, 4 ],             [ 0, 6, 7, 8 ],             [ 9, 10, 11, 12 ],             [ 13, 14, 15, 16 ] ]     mat2 = [ [ 4, 3, 2, 1 ],             [ 8, 7, 6, 0 ],             [ 12, 11, 10, 9 ],             [ 16, 15, 14, 13 ] ]     # Function call    mirrorMatrix(mat1, mat2, N) # This code is contributed by mohit kumar 29
// C# implementation of// the above approachusing System;class GFG{ // Function to check whether the// two matrices are mirror// of each otherstatic void mirrorMatrix(int[,] mat1,                         int [,]mat2, int N){  // Initialising row and column of  // second matrix  int row = 0;  int col = 0;   bool isMirrorImage = true;   // Iterating over the matrices  for (int i = 0; i < N; i++)  {    // Check row of first matrix with    // reversed row of second matrix    for (int j = N - 1; j >= 0; j--)    {      // If the element is not equal      if (mat2[row, col] != mat1[i, j])      {        isMirrorImage = false;      }       // Increment column      col++;    }     // Reset column to 0    // for new row    col = 0;     // Increment row    row++;  }   if (isMirrorImage)    Console.Write("Yes");  else    Console.Write("No");} // Driver codepublic static void Main(String[] args){  // Given 2 matrices  int N = 4;  int [,]mat1 = {{1, 2, 3, 4},                 {0, 6, 7, 8},                 {9, 10, 11, 12},                 {13, 14, 15, 16}};   int [,]mat2 = {{4, 3, 2, 1},                 {8, 7, 6, 0},                 {12, 11, 10, 9},                 {16, 15, 14, 13}};   // Function Call  mirrorMatrix(mat1, mat2, N);}} // This code is contributed by shikhasingrajput
<script> // Javascript implementation of// the above approach // Function to check whether the// two matrices are mirror// of each otherfunction mirrorMatrix(mat1, mat2, N){    // Initialising row and column of    // second matrix    let row = 0;    let col = 0;     let isMirrorImage = true;     // Iterating over the matrices    for (let i = 0; i < N; i++) {         // Check row of first matrix with        // reversed row of second matrix        for (let j = N - 1; j >= 0; j--) {             // If the element is not equal            if (mat2[row][col] != mat1[i][j]) {                isMirrorImage = false;            }             // Increment column            col++;        }         // Reset column to 0        // for new row        col = 0;         // Increment row        row++;    }     if (isMirrorImage)        document.write("Yes");    else        document.write("No");} // Driver code    // Given 2 matrices    let N = 4;    let mat1 = [ [ 1, 2, 3, 4 ],                      [ 0, 6, 7, 8 ],                      [ 9, 10, 11, 12 ],                      [ 13, 14, 15, 16 ] ];     let mat2 = [ [ 4, 3, 2, 1 ],                      [ 8, 7, 6, 0 ],                      [ 12, 11, 10, 9 ],                      [ 16, 15, 14, 13 ] ];     // Function Call    mirrorMatrix(mat1, mat2, N); </script>
Yes
 
Time complexity: O(N2) Auxiliary Space: O(1) 
mohit kumar 29
29AjayKumar
shikhasingrajput
rishavmahato348
Convert to Mirror
Matrix
Searching
Searching
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Flood fill Algorithm - how to implement fill() in paint?
Breadth First Traversal ( BFS ) on a 2D array
Program to find the Sum of each Row and each Column of a Matrix
Python program to add two Matrices
Mathematics | L U Decomposition of a System of Linear Equations
Binary Search
Maximum and minimum of an array using minimum number of comparisons
Linear Search
Find the Missing Number
K'th Smallest/Largest Element in Unsorted Array | Set 1