Check if Matrix remains unchanged after row reversals
Last Updated :
11 May, 2021
Given an NxN matrix. The task is to check if after reversing all the rows of the given Matrix, the matrix remains the same or not.
Examples:
Input : N = 3
1 2 1
2 2 2
3 4 3
Output : Yes
If all the rows are reversed then matrix will become:
1 2 1
2 2 2
3 4 3
which is same.
Input : N = 3
1 2 2
2 2 2
3 4 3
Output : No
Approach:
- A most important observation is for the matrix to be the same after row reversals, each single row must be palindromic.
- Now to check if a row is palindromic, maintain two pointers, one pointing to start and the other to the end of row. Start comparing the values present and do start++ and end–. Repeat the process until all elements are checked till the middle of the row. If at each step elements are the same, then the row is palindromic otherwise not.
- If any of the Row is not palindromic then the answer is No.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
void specialMatrix( int matrix[3][3], int N)
{
for ( int i = 0; i < N; i++) {
int start = 0;
int end = N - 1;
while (start <= end) {
if (matrix[i][start] != matrix[i][end]) {
cout << "No" << endl;
return ;
}
start++;
end--;
}
}
cout << "Yes" << endl;
return ;
}
int main()
{
int matrix[3][3] = { { 1, 2, 1 },
{ 2, 2, 2 },
{ 3, 4, 3 } };
int N = 3;
specialMatrix(matrix, N);
return 0;
}
|
Java
class GFG
{
static void specialMatrix( int matrix[][], int N)
{
for ( int i = 0 ; i < N; i++)
{
int start = 0 ;
int end = N - 1 ;
while (start <= end)
{
if (matrix[i][start] != matrix[i][end])
{
System.out.println( "No" );
return ;
}
start++;
end--;
}
}
System.out.println( "Yes" );
return ;
}
public static void main(String[] args)
{
int matrix[][] = { { 1 , 2 , 1 },
{ 2 , 2 , 2 },
{ 3 , 4 , 3 } };
int N = 3 ;
specialMatrix(matrix, N);
}
}
|
Python3
def specialMatrix(matrix, N):
for i in range (N):
start = 0
end = N - 1
while (start < = end):
if (matrix[i][start] ! = matrix[i][end]):
print ( "No" )
return
start + = 1
end - = 1
print ( "Yes" )
return
if __name__ = = '__main__' :
matrix = [[ 1 , 2 , 1 ], [ 2 , 2 , 2 ], [ 3 , 4 , 3 ]]
N = 3
specialMatrix(matrix, N)
|
C#
using System;
class GFG
{
static void specialMatrix( int [,]matrix, int N)
{
for ( int i = 0; i < N; i++)
{
int start = 0;
int end = N - 1;
while (start <= end)
{
if (matrix[i, start] != matrix[i, end])
{
Console.WriteLine( "No" );
return ;
}
start++;
end--;
}
}
Console.WriteLine( "Yes" );
return ;
}
public static void Main(String[] args)
{
int [,]matrix = { { 1, 2, 1 },
{ 2, 2, 2 },
{ 3, 4, 3 } };
int N = 3;
specialMatrix(matrix, N);
}
}
|
PHP
<?php
function specialMatrix( $matrix , $N )
{
for ( $i = 0; $i < $N ; $i ++)
{
$start = 0;
$end = ( $N - 1);
while ( $start <= $end )
{
if ( $matrix [ $i ][ $start ] != $matrix [ $i ][ $end ])
{
echo "No" , "\n" ;
return ;
}
$start ++;
$end --;
}
}
echo "Yes" , "\n" ;
return ;
}
$matrix = array ( array (1, 2, 1),
array (2, 2, 2),
array (3, 4, 3));
$N = 3;
specialMatrix( $matrix , $N );
?>
|
Javascript
<script>
function specialMatrix(matrix, N)
{
for (let i = 0; i < N; i++)
{
let start = 0;
let end = N - 1;
while (start <= end)
{
if (matrix[i][start] != matrix[i][end])
{
document.write( "No" );
return ;
}
start++;
end--;
}
}
document.write( "Yes" );
return ;
}
let matrix = [ [ 1, 2, 1 ],
[ 2, 2, 2 ],
[ 3, 4, 3 ] ];
let N = 3;
specialMatrix(matrix, N);
</script>
|
Share your thoughts in the comments
Please Login to comment...