Centrosymmetric Matrix
Given a matrix of size n x n. The task is to check whether the given matrix is Centrosymmetric Matrix or not.
In mathematics, a centrosymmetric matrix is a matrix which 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 implemantation of the above approach:
C++
// CPP Program to check whether given // matrix is centrosymmetric or not. #include <bits/stdc++.h> using namespace std; #define N 3 bool checkCentrosymmetricted(int n, int m[N][N]) { int mid_row; // Finding the middle row of the matrix if (n & 1) mid_row = n / 2 + 1; else mid_row = n / 2; // for each row upto middle row. for (int i = 0; i < mid_row; i++) { // If each element and its corresponding // element is not equal then return false. for (int j = 0; j < n; j++) { if (m[i][j] != m[n - i - 1][n - j - 1]) return false; } } return true; } // Driven Program 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
// Java Program to check whether given // matrix is centrosymmetric or not. import java.io.*; class GFG { static int N = 3; static boolean checkCentrosymmetricted( int n, int m[][]) { int mid_row; // Finding the middle row of the // matrix if ((n & 1)>0) mid_row = n / 2 + 1; else mid_row = n / 2; // for each row upto middle row. for (int i = 0; i < mid_row; i++) { // If each element and its // corresponding element is // not equal then return false. for (int j = 0; j < n; j++) { if (m[i][j] != m[n - i - 1][n - j - 1]) return false; } } return true; } // Driven Program 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"); } } // This code is contributed by anuj_67. |
Python3
# Python3 Program to check whether given # matrix is centrosymmetric or not. def checkCentrosymmetricted( n, m): mid_row = 0; # Finding the middle row # of the matrix if ((n & 1) > 0): mid_row = n / 2 + 1; else: mid_row = n / 2; # for each row upto middle row. for i in range(int(mid_row)): # If each element and # its corresponding # element is not equal # then return false. for j in range(n): if (m[i][j] != m[n - i - 1][n - j - 1]): return False; return True; # Driver Code n = 3; m = [[1, 3, 5 ], [ 6, 8, 6 ], [ 5, 3, 1 ]]; if(checkCentrosymmetricted(n, m)): print("Yes"); else: print("No"); # This code is contributed by mits |
C#
// C# Program to check whether given // matrix is centrosymmetric or not. using System; class GFG { ///static int N = 3; static bool checkCentrosymmetricted( int n, int [,]m) { int mid_row; // Finding the middle row of the // matrix if ((n & 1)>0) mid_row = n / 2 + 1; else mid_row = n / 2; // for each row upto middle row. for (int i = 0; i < mid_row; i++) { // If each element and its // corresponding element is // not equal then return false. for (int j = 0; j < n; j++) { if (m[i,j] != m[n - i - 1,n - j - 1]) return false; } } return true; } // Driven Program 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"); } } // This code is contributed by anuj_67. |
PHP
<?php // PHP Program to check whether given // matrix is centrosymmetric or not. //$N = 3; function checkCentrosymmetricted( $n, $m) { $mid_row; // Finding the middle row // of the matrix if ($n & 1) $mid_row = $n / 2 + 1; else $mid_row = $n / 2; // for each row upto middle row. for($i = 0; $i < $mid_row; $i++) { // If each element and // its corresponding // element is not equal // then return false. for($j = 0; $j < $n; $j++) { if ($m[$i][$j] != $m[$n - $i - 1] [$n - $j - 1]) return false; } } return true; } // Driver Code $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"; // This code is contributed by anuj_67. ?> |
Output
Yes
Complexity : O(N2)
Recommended Posts:
- Minimum steps required to convert the matrix into lower hessenberg matrix
- Minimum number of steps to convert a given matrix into Upper Hessenberg matrix
- Minimum number of steps to convert a given matrix into Diagonally Dominant Matrix
- Check if matrix can be converted to another matrix by transposing square sub-matrices
- Program to check diagonal matrix and scalar matrix
- Circular Matrix (Construct a matrix with numbers 1 to m*n in spiral way)
- Maximize sum of N X N upper left sub-matrix from given 2N X 2N matrix
- Program to convert given Matrix to a Diagonal Matrix
- Check if it is possible to make the given matrix increasing matrix or not
- C++ program to Convert a Matrix to Sparse Matrix
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j
- Program to check if a matrix is Binary matrix or not
- Maximum trace possible for any sub-matrix of the given matrix
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Maximum XOR value in matrix
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.



