Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Check if all the diagonals of the Matrix are palindromic or not

  • Difficulty Level : Easy
  • Last Updated : 22 Oct, 2021

Given a matrix mat[][] of dimensions N*M, the task is to check if all the diagonals of the matrix(from top-right to bottom-left) are palindromic or not. If found to be true, then print Yes. Otherwise, print No.

Examples:

Input: mat[][] = [[1, 0, 0, 0], [0, 1, 1, 1], [0, 1, 0, 1], [0, 1, 1, 0]]
Output: Yes
Explanation:
All the diagonals of the matrix mat[][] is given by:

  • {1}
  • {0, 0}
  • {0, 1, 0}
  • {0, 1, 1, 0}
  • {1, 0, 1}
  • {1, 1}
  • {0}

As all the above diagonals are palindromic. Therefore, print Yes.

Input: mat[][] = [[1, 0, 0, 0], [1, 1, 0, 1], [1, 0, 1, 1], [0, 1, 0, 1]]
Output: No

Approach: The given problem can be solved by performing the diagonal traversal of the matrix and for every diagonal traversal check if the elements are palindromic or not. If there exists any such diagonal which is not palindromic, then print Yes. Otherwise, print No.

Below is the implementation of the above approach: 

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
#define N 5
 
// Function to check if the matrix is
// palindrome or not
string isbinaryMatrixPalindrome(
    int mat[N][N])
{
 
    // Traverse the matrix and check if
    // top right and bottom left elements
    // have same value
    for (int i = 0; i < N - 1; i++) {
        for (int j = N - 1; j > i; j--) {
 
            // If top right element is not
            // equal to the bottom left
            // element return false
            if (mat[i][j] != mat[j][i]) {
                return "Np";
            }
        }
    }
 
    return "Yes";
}
 
// Driver Code
int main()
{
    int mat[N][N] = { { 1, 0, 0, 1, 1 },
                      { 0, 1, 0, 1, 0 },
                      { 0, 0, 1, 1, 1 },
                      { 1, 1, 1, 0, 1 },
                      { 1, 0, 1, 1, 0 } };
 
    cout << isbinaryMatrixPalindrome(mat);
 
    return 0;
}

Java




// Java program for the above approach
 
public class GFG {
     
    final static int N = 5;
     
    // Function to check if the matrix is
    // palindrome or not
    static String isbinaryMatrixPalindrome(int mat[][])
    {
     
        // Traverse the matrix and check if
        // top right and bottom left elements
        // have same value
        for (int i = 0; i < N - 1; i++) {
            for (int j = N - 1; j > i; j--) {
     
                // If top right element is not
                // equal to the bottom left
                // element return false
                if (mat[i][j] != mat[j][i]) {
                    return "Np";
                }
            }
        }
     
        return "Yes";
    }
     
    // Driver Code
    public static void main (String[] args) {
         
        int mat[][] = { { 1, 0, 0, 1, 1 },
                          { 0, 1, 0, 1, 0 },
                          { 0, 0, 1, 1, 1 },
                          { 1, 1, 1, 0, 1 },
                          { 1, 0, 1, 1, 0 } };
     
        System.out.println(isbinaryMatrixPalindrome(mat));
    }
}
 
// This code is contributed by AnkThon

Python3




# python program for the above approach
N = 5
 
# Function to check if the matrix is
# palindrome or not
def isbinaryMatrixPalindrome(mat):
 
    # Traverse the matrix and check if
    # top right and bottom left elements
    # have same value
    for i in range(0, N - 1):
        for j in range(N - 1, i, -1):
 
            # If top right element is not
            # equal to the bottom left
            # element return false
            if (mat[i][j] != mat[j][i]):
                return "No"
 
    return "Yes"
 
# Driver Code
if __name__ == "__main__":
 
    mat = [[1, 0, 0, 1, 1],
           [0, 1, 0, 1, 0],
           [0, 0, 1, 1, 1],
           [1, 1, 1, 0, 1],
           [1, 0, 1, 1, 0]]
    print(isbinaryMatrixPalindrome(mat))
 
    # This code is contributed by rakeshsahni

C#




// C# program for the above approach
using System;
public class GFG {
     
    static int N = 5;
     
    // Function to check if the matrix is
    // palindrome or not
    static string isbinaryMatrixPalindrome(int [,]mat)
    {
     
        // Traverse the matrix and check if
        // top right and bottom left elements
        // have same value
        for (int i = 0; i < N - 1; i++) {
            for (int j = N - 1; j > i; j--) {
     
                // If top right element is not
                // equal to the bottom left
                // element return false
                if (mat[i, j] != mat[j, i]) {
                    return "Np";
                }
            }
        }
     
        return "Yes";
    }
     
    // Driver Code
    public static void Main (string[] args) {
         
        int [,]mat = { { 1, 0, 0, 1, 1 },
                          { 0, 1, 0, 1, 0 },
                          { 0, 0, 1, 1, 1 },
                          { 1, 1, 1, 0, 1 },
                          { 1, 0, 1, 1, 0 } };
     
        Console.WriteLine(isbinaryMatrixPalindrome(mat));
    }
}
 
// This code is contributed by ukasp.

Javascript




<script>
// Javascript program for the above approach
 
let N = 5;
 
// Function to check if the matrix is
// palindrome or not
function isbinaryMatrixPalindrome(mat)
{
 
  // Traverse the matrix and check if
  // top right and bottom left elements
  // have same value
  for (let i = 0; i < N - 1; i++)
  {
    for (let j = N - 1; j > i; j--)
    {
     
      // If top right element is not
      // equal to the bottom left
      // element return false
      if (mat[i][j] != mat[j][i]) {
        return "Np";
      }
    }
  }
 
  return "Yes";
}
 
// Driver Code
 
let mat = [
  [1, 0, 0, 1, 1],
  [0, 1, 0, 1, 0],
  [0, 0, 1, 1, 1],
  [1, 1, 1, 0, 1],
  [1, 0, 1, 1, 0],
];
 
document.write(isbinaryMatrixPalindrome(mat));
 
// This code is contributed by saurabh_jaiswal.
</script>

 
 

Output: 
Yes

 

 

Time Complexity: O(N2)
Auxiliary Space: O(1)

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!