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

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check whether the
// two matrices are mirror
// of each other
void 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 code
int 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);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of
// the above approach
import java.util.*;
class GFG{
 
// Function to check whether the
// two matrices are mirror
// of each other
static 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 code
public 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of
# the above approach
 
# Function to check whether the
# two matrices are mirror
# of each other
def 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 code
if __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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of
// the above approach
using System;
class GFG{
 
// Function to check whether the
// two matrices are mirror
// of each other
static 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 code
public 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

chevron_right


Output: 

Yes






 

Time complexity: O(N2
Auxiliary Space: O(1)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.