Problem Statement
Write a program to check whether the given matrix is palindrome or not.
Solution
#include<stdio.h> //header file for printf & scanf function
int main()
{
int n,m,i,j,p,q,k;
scanf("%d%d",&n,&m); //getting row and column value
int a[n][m];
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
if(i!=n-1)
scanf("\n");
}
if(m%2==0) //checking for column value is even or odd
p=m/2;
else
p=(m+1)/2;
if(n%2==0) //checking for row value is even or odd
q=n/2;
else
q=(n+1)/2;
for(i=0;i<n;i++) //This nested for loop is for checking column palindrome
{
for(j=0,k=m-1;j<p;j++,k--)
{
if(a[i][j]!=a[i][k]) // if not equal print no and exit of the program
{
printf("No");
return 0;
}
}
}
for(i=0;i<m;i++) //This nested for loop is for checking row palindrome
{
for(j=0,k=n-1;j<q;j++,k--)
{
if(a[j][i]!=a[k][i]) // if not equal print no and exit of the program
{
printf("No");
return 0;
}
}
}
printf("Yes"); // if both nested for loops successfully executed then it will print yes.
}
Note:
we have to check for every row and column palindrome
Input 1
5 4
67 77 77 67
48 74 74 48
53 95 95 53
48 74 74 48
67 77 77 67
Output 1
Yes
Input 2
3 5
2 4 3 4 2
3 7 8 7 3
5 6 1 65
Output
NO