Skip to content
Related Articles

Related Articles

Improve Article

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:  

Attention reader! All those who say programming isn't for kids, just haven't met the right mentors yet. Join the  Demo Class for First Step to Coding Coursespecifically designed for students of class 8 to 12. 

The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.

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 the 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 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.
  3. If any of the Row is not palindromic then the answer is No.

Below is the implementation of the above approach:  

C++




// 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;
}

Java




// 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 */

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

C#




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

PHP




<?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.
?>

Javascript




<script>
    // Javascript implementation of the above approach
     
    // Function to check Palindromic Condition
    function specialMatrix(matrix, N)
    {
        for (let i = 0; i < N; i++)
        {
 
            // Pointer to start of row
            let start = 0;
 
            // Pointer to end of row
            let end = N - 1;
 
            while (start <= end)
            {
 
                // Single Mismatch means
                // row is not palindromic
                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>
Output: 
Yes

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :