Check if Matrix remains unchanged after row reversals

Given a NxN matrix. The task is to check if after reversing all of the rows of the given Matrix, the matrix remains 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:

  1. A most important observation is for the matrix to be same after row reversals, each single row must be palindromic.
  2. Now to check if a row is palindromic, maintain two pointers, one pointing to start and other to 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 same, then row is palindromic otherwise not.
  3. If any of the Row is not palindromic then answer is No.

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>
#define ll long long int
using namespace std;
  
// Function to check Palindromic Condition
void specialMatrix(int matrix[3][3], int N)
{
    for (int i = 0; i < N; i++) {
  
        // Pointer to start of row
        int start = 0;
  
        // Pointer to end of row
        int end = N - 1;
  
        while (start <= end) {
  
            // Single Mismatch means row is not palindromic
            if (matrix[i][start] != matrix[i][end]) {
                cout << "No" << endl;
                return;
            }
  
            start++;
            end--;
        }
    }
  
    cout << "Yes" << endl;
    return;
}
  
// Driver Code
int main()
{
    int matrix[3][3] = { { 1, 2, 1 },
                         { 2, 2, 2 },
                         { 3, 4, 3 } };
    int N = 3;
    specialMatrix(matrix, N);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach
class GFG 
{
  
// Function to check Palindromic Condition
static void specialMatrix(int matrix[][], int N)
{
    for (int i = 0; i < N; i++) 
    {
  
        // Pointer to start of row
        int start = 0;
  
        // Pointer to end of row
        int end = N - 1;
  
        while (start <= end) 
        {
  
            // Single Mismatch means 
            // row is not palindromic
            if (matrix[i][start] != matrix[i][end])
            {
                System.out.println("No");
                return;
            }
  
            start++;
            end--;
        }
    }
  
    System.out.println("Yes");
    return;
}
  
// Driver Code
public static void main(String[] args) 
{
    int matrix[][] = { { 1, 2, 1 },
                        { 2, 2, 2 },
                        { 3, 4, 3 } };
    int N = 3;
    specialMatrix(matrix, N);
}
}
  
/* This code contributed by PrinciRaj1992 */

chevron_right


Python3

# Python 3 implementation of the above approach

# Function to check Palindromic Condition
def specialMatrix(matrix, N):
for i in range(N):

# Pointer to start of row
start = 0

# Pointer to end of row
end = N – 1

while (start <= end): # Single Mismatch means row is not palindromic if (matrix[i][start] != matrix[i][end]): print("No") return start += 1 end -= 1 print("Yes") return # Driver Code if __name__ == '__main__': matrix = [[1, 2, 1], [2, 2, 2], [3, 4, 3]] N = 3 specialMatrix(matrix, N) # This code is contributed by # Surendra_Gangwar [tabby title="C#"]

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach
using System;
  
class GFG 
{
  
// Function to check Palindromic Condition
static void specialMatrix(int [,]matrix, int N)
{
    for (int i = 0; i < N; i++) 
    {
  
        // Pointer to start of row
        int start = 0;
  
        // Pointer to end of row
        int end = N - 1;
  
        while (start <= end) 
        {
  
            // Single Mismatch means 
            // row is not palindromic
            if (matrix[i, start] != matrix[i, end])
            {
                Console.WriteLine("No");
                return;
            }
  
            start++;
            end--;
        }
    }
    Console.WriteLine("Yes");
    return;
}
  
// Driver Code
public static void Main(String[] args) 
{
    int [,]matrix = { { 1, 2, 1 },
                        { 2, 2, 2 },
                        { 3, 4, 3 } };
    int N = 3;
    specialMatrix(matrix, N);
}
}
  
// This code has been contributed by 29AjayKumar

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the above approach
  
// Function to check Palindromic Condition
function specialMatrix($matrix, $N)
{
    for ($i = 0; $i < $N; $i++) 
    {
  
        // Pointer to start of row
        $start = 0;
  
        // Pointer to end of row
        $end = ($N - 1);
  
        while ($start <= $end
        {
  
            // Single Mismatch means row is not palindromic
            if ($matrix[$i][$start] != $matrix[$i][$end])
            {
                echo "No", "\n";
                return;
            }
  
            $start++;
            $end--;
        }
    }
  
    echo "Yes", "\n";
    return;
}
  
// Driver Code
$matrix = array(array(1, 2, 1),
                array(2, 2, 2),
                array(3, 4, 3));
$N = 3;
specialMatrix($matrix, $N);
  
// This code is contributed by ajit.
?>

chevron_right


Output:

Yes


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.