Check if the given two matrices are mirror images of one another
Last Updated :
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++
#include <bits/stdc++.h>
using namespace std;
void mirrorMatrix( int mat1[][4],
int mat2[][4], int N)
{
int row = 0;
int col = 0;
bool isMirrorImage = true ;
for ( int i = 0; i < N; i++) {
for ( int j = N - 1; j >= 0; j--) {
if (mat2[row][col] != mat1[i][j]) {
isMirrorImage = false ;
}
col++;
}
col = 0;
row++;
}
if (isMirrorImage)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
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 } };
mirrorMatrix(mat1, mat2, N);
}
|
Java
import java.util.*;
class GFG{
static void mirrorMatrix( int mat1[][],
int mat2[][], int N)
{
int row = 0 ;
int col = 0 ;
boolean isMirrorImage = true ;
for ( int i = 0 ; i < N; i++)
{
for ( int j = N - 1 ; j >= 0 ; j--)
{
if (mat2[row][col] != mat1[i][j])
{
isMirrorImage = false ;
}
col++;
}
col = 0 ;
row++;
}
if (isMirrorImage)
System.out.print( "Yes" );
else
System.out.print( "No" );
}
public static void main(String[] args)
{
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 }};
mirrorMatrix(mat1, mat2, N);
}
}
|
Python3
def mirrorMatrix(mat1, mat2, N):
row = 0
col = 0
isMirrorImage = True
for i in range (N):
for j in range (N - 1 , - 1 , - 1 ):
if (mat2[row][col] ! = mat1[i][j]):
isMirrorImage = False
col + = 1
col = 0
row + = 1
if (isMirrorImage):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
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 ] ]
mirrorMatrix(mat1, mat2, N)
|
C#
using System;
class GFG{
static void mirrorMatrix( int [,] mat1,
int [,]mat2, int N)
{
int row = 0;
int col = 0;
bool isMirrorImage = true ;
for ( int i = 0; i < N; i++)
{
for ( int j = N - 1; j >= 0; j--)
{
if (mat2[row, col] != mat1[i, j])
{
isMirrorImage = false ;
}
col++;
}
col = 0;
row++;
}
if (isMirrorImage)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
public static void Main(String[] args)
{
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}};
mirrorMatrix(mat1, mat2, N);
}
}
|
Javascript
<script>
function mirrorMatrix(mat1, mat2, N)
{
let row = 0;
let col = 0;
let isMirrorImage = true ;
for (let i = 0; i < N; i++) {
for (let j = N - 1; j >= 0; j--) {
if (mat2[row][col] != mat1[i][j]) {
isMirrorImage = false ;
}
col++;
}
col = 0;
row++;
}
if (isMirrorImage)
document.write( "Yes" );
else
document.write( "No" );
}
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 ] ];
mirrorMatrix(mat1, mat2, N);
</script>
|
Time complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...