# Check if row-major order path of Matrix is palindrome or not

Given a matrix mat[][], the task is to check if the row-major order path of the matrix is a palindrome or not.

Examples:

Input: mat[][] = {{1, 2, 3}, {4, 5, 4}, {3, 2, 1}}
Output: YES
Explanation:
Row-major order traversal of matrix is –
1 => 2 => 3 => 4 => 5 => 4 => 3 => 2 => 1
Reverse row-major order traversal of matrix is –
1 => 2 => 3 => 4 => 5 => 4 => 3 => 2 => 1
Since, the reverse and forward traversal of matrix is same.
Therefore, it is a palindrome.

Input: mat[][] = {{1, 2}, {2, 3}}
Output: NO

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to traverse the half matrix and check its opposite half elements are the same or not at the same time. That is for the index of i and j, check that and . If for any index there is a mismatch, print NO.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to check if ` `// row-major order traversal of ` `// matrix is palindrome or not ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to check if row-major order ` `// traversal of the matrix is is palindrome ` `bool` `isPal(``int` `a, ``int` `n, ``int` `m) ` `{ ` `    ``// Loop to check if the matrix is ` `    ``// matrix is palindrome or not ` `    ``for` `(``int` `i = 0; i < n / 2; i++) { ` `        ``for` `(``int` `j = 0; j < m - 1; j++) { ` `            ``if` `(a[i][j] != a[n - 1 - i][m - 1 - j]) ` `                ``return` `false``; ` `        ``} ` `    ``} ` `    ``return` `true``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 3, m = 3; ` `    ``int` `a = { { 1, 2, 3 }, ` `                    ``{ 4, 5, 4 }, ` `                    ``{ 3, 2, 1 } }; ` `    ``if` `(isPal(a, n, m)) { ` `        ``cout << ``"YES"` `<< endl; ` `    ``} ` `    ``else` `{ ` `        ``cout << ``"NO"` `<< endl; ` `    ``} ` `} `

## Java

 `// Java implementation to check if ` `// row-major order traversal of ` `// matrix is palindrome or not ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `// Function to check if row-major order ` `// traversal of the matrix is is palindrome ` `static` `boolean` `isPal(``int` `a[][], ``int` `n, ``int` `m) ` `{ ` `     `  `    ``// Loop to check if the matrix is ` `    ``// matrix is palindrome or not ` `    ``for``(``int` `i = ``0``; i < n / ``2``; i++) ` `    ``{ ` `       ``for``(``int` `j = ``0``; j < m - ``1``; j++) ` `       ``{ ` `          ``if` `(a[i][j] != a[n - ``1` `- i][m - ``1` `- j]) ` `              ``return` `false``; ` `       ``} ` `    ``} ` `    ``return` `true``; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `n = ``3``, m = ``3``; ` `    ``int` `a[][] = { { ``1``, ``2``, ``3` `}, ` `                  ``{ ``4``, ``5``, ``4` `}, ` `                  ``{ ``3``, ``2``, ``1` `} }; ` `                   `  `    ``if` `(isPal(a, n, m)) ` `    ``{ ` `        ``System.out.print(``"YES"` `+ ``"\n"``); ` `    ``} ` `    ``else` `    ``{ ` `        ``System.out.print(``"NO"` `+ ``"\n"``); ` `    ``} ` `} ` `} ` ` `  `// This code is contributed by gauravrajput1 `

## Python3

 `# Python3 implementation to check if ` `# row-major order traversal of ` `# matrix is palindrome or not ` ` `  `# Function to check if row-major order ` `# traversal of the matrix is is palindrome ` `def` `isPal(a, n, m): ` `     `  `    ``# Loop to check if the matrix is ` `    ``# matrix is palindrome or not ` `    ``for` `i ``in` `range``(``0``, n ``/``/` `2``): ` `        ``for` `j ``in` `range``(``0``, m ``-` `1``): ` `            ``if` `(a[i][j] !``=` `a[n ``-` `1` `-` `i][m ``-` `1` `-` `j]): ` `                ``return` `False``; ` ` `  `    ``return` `True``; ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``n ``=` `3``; ` `    ``m ``=` `3``; ` `    ``a ``=` `[[``1``, ``2``, ``3``], [``4``, ``5``, ``4``], [``3``, ``2``, ``1``]]; ` ` `  `    ``if` `(isPal(a, n, m)): ` `        ``print``(``"YES"``); ` `    ``else``: ` `        ``print``(``"NO"``); ` ` `  `# This code is contributed by Princi Singh `

## C#

 `// C# implementation to check if ` `// row-major order traversal of ` `// matrix is palindrome or not ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `// Function to check if row-major order ` `// traversal of the matrix is is palindrome ` `static` `bool` `isPal(``int``[,]a, ``int` `n, ``int` `m) ` `{ ` `     `  `    ``// Loop to check if the matrix is ` `    ``// matrix is palindrome or not ` `    ``for``(``int` `i = 0; i < n / 2; i++) ` `    ``{ ` `       ``for``(``int` `j = 0; j < m - 1; j++) ` `       ``{ ` `           ``if` `(a[i, j] != a[n - 1 - i,  ` `                            ``m - 1 - j]) ` `               ``return` `false``; ` `       ``} ` `    ``} ` `    ``return` `true``; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `n = 3, m = 3; ` `    ``int``[,]a = { { 1, 2, 3 }, ` `                ``{ 4, 5, 4 }, ` `                ``{ 3, 2, 1 } }; ` `                     `  `    ``if` `(isPal(a, n, m)) ` `    ``{ ` `        ``Console.Write(``"YES"` `+ ``"\n"``); ` `    ``} ` `    ``else` `    ``{ ` `        ``Console.Write(``"NO"` `+ ``"\n"``); ` `    ``} ` `} ` `} ` ` `  `// This code is contributed by gauravrajput1 `

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.