Check if all the diagonals of the Matrix are palindromic or not
Last Updated :
12 Apr, 2023
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++
#include <bits/stdc++.h>
using namespace std;
#define N 5
string isbinaryMatrixPalindrome(
int mat[N][N])
{
for ( int i = 0; i < N - 1; i++) {
for ( int j = N - 1; j > i; j--) {
if (mat[i][j] != mat[j][i]) {
return "Np" ;
}
}
}
return "Yes" ;
}
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
public class GFG {
final static int N = 5 ;
static String isbinaryMatrixPalindrome( int mat[][])
{
for ( int i = 0 ; i < N - 1 ; i++) {
for ( int j = N - 1 ; j > i; j--) {
if (mat[i][j] != mat[j][i]) {
return "Np" ;
}
}
}
return "Yes" ;
}
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));
}
}
|
Python3
N = 5
def isbinaryMatrixPalindrome(mat):
for i in range ( 0 , N - 1 ):
for j in range (N - 1 , i, - 1 ):
if (mat[i][j] ! = mat[j][i]):
return "No"
return "Yes"
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))
|
C#
using System;
public class GFG {
static int N = 5;
static string isbinaryMatrixPalindrome( int [,]mat)
{
for ( int i = 0; i < N - 1; i++) {
for ( int j = N - 1; j > i; j--) {
if (mat[i, j] != mat[j, i]) {
return "Np" ;
}
}
}
return "Yes" ;
}
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));
}
}
|
Javascript
<script>
let N = 5;
function isbinaryMatrixPalindrome(mat)
{
for (let i = 0; i < N - 1; i++)
{
for (let j = N - 1; j > i; j--)
{
if (mat[i][j] != mat[j][i]) {
return "Np" ;
}
}
}
return "Yes" ;
}
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));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Another Approach:
- Traverse each diagonal of the matrix from top-right to bottom-left.
- For each diagonal, create a temporary string and append each element in the diagonal to it.
- Check if the temporary string is a palindrome. If not, return “No”.
- If all diagonals are palindromic, return “Yes”.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <string>
using namespace std;
#define N 4
#define M 4
bool isPalindrome(string s) {
int n = s.length();
for ( int i = 0; i < n/2; i++) {
if (s[i] != s[n-i-1]) {
return false ;
}
}
return true ;
}
string isDiagonalPalindrome( int mat[N][M]) {
for ( int i = 0; i < N; i++) {
string s = "" ;
int x = i, y = M-1;
while (x < N && y >= 0) {
s += to_string(mat[x][y]);
x++;
y--;
}
if (!isPalindrome(s)) {
return "No" ;
}
}
for ( int j = M-1; j >= 0; j--) {
string s = "" ;
int x = 0, y = j;
while (x < N && y >= 0) {
s += to_string(mat[x][y]);
x++;
y--;
}
if (!isPalindrome(s)) {
return "No" ;
}
}
return "Yes" ;
}
int main() {
int mat[N][M] = {{1, 0, 0, 0},
{0, 1, 1, 1},
{0, 1, 0, 1},
{0, 1, 1, 0}};
cout << isDiagonalPalindrome(mat) << endl;
return 0;
}
|
Java
import java.util.*;
class Main {
static final int N = 4 ;
static final int M = 4 ;
static boolean isPalindrome(String s) {
int n = s.length();
for ( int i = 0 ; i < n/ 2 ; i++) {
if (s.charAt(i) != s.charAt(n-i- 1 )) {
return false ;
}
}
return true ;
}
static String isDiagonalPalindrome( int [][] mat) {
for ( int i = 0 ; i < N; i++) {
StringBuilder s = new StringBuilder();
int x = i, y = M- 1 ;
while (x < N && y >= 0 ) {
s.append(mat[x][y]);
x++;
y--;
}
if (!isPalindrome(s.toString())) {
return "No" ;
}
}
for ( int j = M- 1 ; j >= 0 ; j--) {
StringBuilder s = new StringBuilder();
int x = 0 , y = j;
while (x < N && y >= 0 ) {
s.append(mat[x][y]);
x++;
y--;
}
if (!isPalindrome(s.toString())) {
return "No" ;
}
}
return "Yes" ;
}
public static void main(String[] args) {
int [][] mat = {{ 1 , 0 , 0 , 0 },
{ 0 , 1 , 1 , 1 },
{ 0 , 1 , 0 , 1 },
{ 0 , 1 , 1 , 0 }};
System.out.println(isDiagonalPalindrome(mat));
}
}
|
Python3
def isPalindrome(s):
n = len (s)
for i in range (n / / 2 ):
if s[i] ! = s[n - i - 1 ]:
return False
return True
def isDiagonalPalindrome(mat):
for i in range (N):
s = ""
x, y = i, M - 1
while x < N and y > = 0 :
s + = str (mat[x][y])
x + = 1
y - = 1
if not isPalindrome(s):
return "No"
for j in range (M - 1 , - 1 , - 1 ):
s = ""
x, y = 0 , j
while x < N and y > = 0 :
s + = str (mat[x][y])
x + = 1
y - = 1
if not isPalindrome(s):
return "No"
return "Yes"
if __name__ = = '__main__' :
N, M = 4 , 4
mat = [[ 1 , 0 , 0 , 0 ],
[ 0 , 1 , 1 , 1 ],
[ 0 , 1 , 0 , 1 ],
[ 0 , 1 , 1 , 0 ]]
print (isDiagonalPalindrome(mat))
|
C#
using System;
public class MainClass
{
public static bool IsPalindrome( string s)
{
int n = s.Length;
for ( int i = 0; i < n / 2; i++) {
if (s[i] != s[n - i - 1]) {
return false ;
}
}
return true ;
}
public static string IsDiagonalPalindrome( int [, ] mat)
{
int N = mat.GetLength(0);
int M = mat.GetLength(1);
for ( int i = 0; i < N; i++) {
string s = "" ;
int x = i, y = M - 1;
while (x < N && y >= 0) {
s += mat[x, y].ToString();
x++;
y--;
}
if (!IsPalindrome(s)) {
return "No" ;
}
}
for ( int j = M - 1; j >= 0; j--) {
string s = "" ;
int x = 0, y = j;
while (x < N && y >= 0) {
s += mat[x, y].ToString();
x++;
y--;
}
if (!IsPalindrome(s)) {
return "No" ;
}
}
return "Yes" ;
}
public static void Main()
{
int [, ] mat = { { 1, 0, 0, 0 },
{ 0, 1, 1, 1 },
{ 0, 1, 0, 1 },
{ 0, 1, 1, 0 } };
Console.WriteLine(IsDiagonalPalindrome(mat));
}
}
|
Javascript
function isPalindrome(s) {
let n = s.length;
for (let i = 0; i < n / 2; i++) {
if (s[i] != s[n - i - 1]) {
return false ;
}
}
return true ;
}
function isDiagonalPalindrome(mat) {
for (let i = 0; i < N; i++) {
let s = "" ;
let x = i,
y = M - 1;
while (x < N && y >= 0) {
s += mat[x][y].toString();
x++;
y--;
}
if (!isPalindrome(s)) {
return "No" ;
}
}
for (let j = M - 1; j >= 0; j--) {
let s = "" ;
let x = 0,
y = j;
while (x < N && y >= 0) {
s += mat[x][y].toString();
x++;
y--;
}
if (!isPalindrome(s)) {
return "No" ;
}
}
return "Yes" ;
}
const N = 4,
M = 4;
let mat = [
[1, 0, 0, 0],
[0, 1, 1, 1],
[0, 1, 0, 1],
[0, 1, 1, 0]
];
console.log(isDiagonalPalindrome(mat));
|
Time complexity: O(NM^2)
Auxiliary Space: O(NM)
Share your thoughts in the comments
Please Login to comment...