Given a matrix of size n x n. The task is to check whether the given matrix is a Centrosymmetric Matrix or not.
In mathematics, a centrosymmetric matrix is a matrix that is symmetric about its center.
Examples:
Input : n = 3,
m[][] = { { 1, 3, 5 },
{ 6, 8, 6 },
{ 5, 3, 1 } };
Output : Yes
Input : n = 3,
m[][] = { { 1, 3, 5 },
{ 6, 8, 6 },
{ 5, 3, 0 } };
Output : No
The idea is to check for each element whether m[i][j] is equal to m[n – i – 1][n – j – 1]. If any element does not satisfy the above condition, then print “No”, otherwise print “Yes”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 3
bool checkCentrosymmetricted( int n, int m[N][N])
{
int mid_row;
if (n & 1)
mid_row = n / 2 + 1;
else
mid_row = n / 2;
for ( int i = 0; i < mid_row; i++) {
for ( int j = 0; j < n; j++) {
if (m[i][j] != m[n - i - 1][n - j - 1])
return false ;
}
}
return true ;
}
int main()
{
int n = 3;
int m[N][N] = { { 1, 3, 5 },
{ 6, 8, 6 },
{ 5, 3, 1 } };
(checkCentrosymmetricted(n, m) ?
(cout << "Yes" ) : (cout << "No" ));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int N = 3 ;
static boolean checkCentrosymmetricted(
int n, int m[][])
{
int mid_row;
if ((n & 1 )> 0 )
mid_row = n / 2 + 1 ;
else
mid_row = n / 2 ;
for ( int i = 0 ; i < mid_row; i++)
{
for ( int j = 0 ; j < n; j++)
{
if (m[i][j] !=
m[n - i - 1 ][n - j - 1 ])
return false ;
}
}
return true ;
}
public static void main (String[] args)
{
int n = 3 ;
int m[][] = { { 1 , 3 , 5 },
{ 6 , 8 , 6 },
{ 5 , 3 , 1 } };
if (checkCentrosymmetricted(n, m))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def checkCentrosymmetricted( n, m):
mid_row = 0 ;
if ((n & 1 ) > 0 ):
mid_row = n / 2 + 1 ;
else :
mid_row = n / 2 ;
for i in range ( int (mid_row)):
for j in range (n):
if (m[i][j] ! = m[n - i - 1 ][n - j - 1 ]):
return False ;
return True ;
n = 3 ;
m = [[ 1 , 3 , 5 ],
[ 6 , 8 , 6 ],
[ 5 , 3 , 1 ]];
if (checkCentrosymmetricted(n, m)):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG {
///static int N = 3;
static bool checkCentrosymmetricted(
int n, int [,]m)
{
int mid_row;
if ((n & 1)>0)
mid_row = n / 2 + 1;
else
mid_row = n / 2;
for ( int i = 0; i < mid_row; i++)
{
for ( int j = 0; j < n; j++)
{
if (m[i,j] !=
m[n - i - 1,n - j - 1])
return false ;
}
}
return true ;
}
public static void Main()
{
int n = 3;
int [,]m = { { 1, 3, 5 },
{ 6, 8, 6 },
{ 5, 3, 1 } };
if (checkCentrosymmetricted(n, m))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function checkCentrosymmetricted( $n , $m )
{
$mid_row ;
if ( $n & 1)
$mid_row = $n / 2 + 1;
else
$mid_row = $n / 2;
for ( $i = 0; $i < $mid_row ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++) {
if ( $m [ $i ][ $j ] != $m [ $n - $i - 1]
[ $n - $j - 1])
return false;
}
}
return true;
}
$n = 3;
$m = array ( array (1, 3, 5 ),
array ( 6, 8, 6 ),
array ( 5, 3, 1 ));
if (checkCentrosymmetricted( $n , $m ) )
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
let N = 3
function checkCentrosymmetricted( n, m)
{
let mid_row;
if (n & 1)
mid_row = Math.floor(n / 2) + 1;
else
mid_row = n / 2;
for (let i = 0; i < mid_row; i++) {
for (let j = 0; j < n; j++) {
if (m[i][j] != m[n - i - 1][n - j - 1])
return false ;
}
}
return true ;
}
let n = 3;
let m = [ [ 1, 3, 5 ],
[ 6, 8, 6 ],
[ 5, 3, 1 ] ];
(checkCentrosymmetricted(n, m) ?
(document.write( "Yes" )) : document.write( "No" ));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Last Updated :
05 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...