Bisymmetric matrix
A Bisymmetric matrix is a square matrix that is symmetric about both of its main diagonals. Given a matrix of size n x n. The task is to check if its is bisymmetric or not.
Examples :
Input : n = 3, m[][] = { { 1, 2, 3 },
{ 2, 5, 2 },
{ 3, 2, 1 } };
Output : Yes
Given matrix is symmetric along both the diagonal.
Input : n = 3, m[][] = { { 1, 2, 3 },
{ 9, 5, 2 },
{ 3, 2, 1 } };
Output : No
The idea is to check if the given matrix is symmetric along both the diagonal or not. For diagonal from top-left to bottom-right, check if element at m[i][j] is equal to m[j][i]. For diagonal from top-right to bottom-left, check if element at m[i][j] is equal to m[n – j – 1][n – i – 1].
If both the above condition is true, then the given matrix is Bisymmetric matrix, otherwise not.
C++
// CPP Program to check if a // given matrix is Bisymmetric // matrix #include <bits/stdc++.h> using namespace std; #define MAX 100 // Return if the given // matrix is Bisymmetric matrix bool checkBisymmetric(int m[][MAX], int n) { // Checking Across // Forward Diagonal for (int i = 0; i < n; i++) for (int j = 0; j < i; j++) // check if corresponding // position element are equal. if (m[i][j] != m[j][i]) return false; // Backward Diagonal for (int i = 0; i < n; i++) // for each column, // upto main diagonal for (int j = 0; j < n - i; j++) // check if corresponding // position element are equal. if (m[i][j] != m[n - j - 1] [n - i - 1]) return false; return true; } // Driven Code int main() { int n = 3; int m[][MAX] = { { 1, 2, 3 }, { 2, 5, 2 }, { 3, 2, 1 } }; (checkBisymmetric(m, n) ? (cout << "Yes") : (cout << "No")); return 0; } |
Java
// Java Program to check if a given matrix // is Bisymmetric matrix import java.io.*; class GFG { static int MAX = 100; // Return if the given // matrix is Bisymmetric matrix static boolean checkBisymmetric(int m[][], int n) { // Checking Across Forward Diagonal for (int i = 0; i < n; i++) for (int j = 0; j < i; j++) // check if corresponding // position element are equal. if (m[i][j] != m[j][i]) return false; // Backward Diagonal for (int i = 0; i < n; i++) // for each column, // upto main diagonal for (int j = 0; j < n - i; j++) // check if corresponding // position element are equal. if (m[i][j] != m[n - j - 1] [n - i - 1]) return false; return true; } // Driven Code public static void main (String[] args) { int n = 3; int m[][] = { { 1, 2, 3 }, { 2, 5, 2 }, { 3, 2, 1 } }; if(checkBisymmetric(m, n)) System.out.println( "Yes"); else System.out.println( "No"); } } // This code is contributed by anuj_67. |
Python3
# Python3 Program to check if a # given matrix is Bisymmetric # matrix # Return if the given matrix # is Bisymmetric matrix def checkBisymmetric(m, n) : # Checking Across # Forward Diagonal for i in range(0, n) : for j in range(0, i) : # check if corresponding # position element are equal. if (m[i][j] != m[j][i]) : return false # Backward Diagonal for i in range(0, n) : # for each column, # upto main diagonal for j in range(0, n - i) : # check if corresponding # position element are # equal. if (m[i][j] != m[n - j - 1][n - i - 1]) : return False return True; # Driven Code n = 3; m = [[ 1, 2, 3 ], [ 2, 5, 2 ], [ 3, 2, 1 ]] if(checkBisymmetric(m, n)) : print ("Yes") else : print ("No") # This code is contributed by # Manish Shaw (manishshaw1) |
C#
// C# Program to check if a given matrix // is Bisymmetric matrix using System; class GFG { //static int MAX = 100; // Return if the given // matrix is Bisymmetric matrix static bool checkBisymmetric(int [,]m, int n) { // Checking Across Forward Diagonal for (int i = 0; i < n; i++) for (int j = 0; j < i; j++) // check if corresponding // position element are equal. if (m[i,j] != m[j,i]) return false; // Backward Diagonal for (int i = 0; i < n; i++) // for each column, // upto main diagonal for (int j = 0; j < n - i; j++) // check if corresponding // position element are equal. if (m[i,j] != m[n - j - 1, n - i - 1]) return false; return true; } // Driven Code public static void Main () { int n = 3; int [,]m = { { 1, 2, 3 }, { 2, 5, 2 }, { 3, 2, 1 } }; if(checkBisymmetric(m, n)) Console.WriteLine( "Yes"); else Console.WriteLine( "No"); } } // This code is contributed by anuj_67. |
PHP
<?php // PHP Program to check if a // given matrix is Bisymmetric // matrix // MAX = 100; // Return if the given matrix // is Bisymmetric matrix function checkBisymmetric($m, $n) { // Checking Across // Forward Diagonal for ( $i = 0; $i < $n; $i++) for ( $j = 0; $j < $i; $j++) // check if corresponding // position element are equal. if ($m[$i][$j] != $m[$j][$i]) return false; // Backward Diagonal for ( $i = 0; $i < $n; $i++) // for each column, // upto main diagonal for ( $j = 0; $j < $n - $i; $j++) // check if corresponding // position element are equal. if ($m[$i][$j] != $m[$n - $j - 1] [$n - $i - 1]) return false; return true; } // Driven Code $n = 3; $m = array(array( 1, 2, 3 ), array( 2, 5, 2 ), array( 3, 2, 1 )); if(checkBisymmetric($m, $n)) echo "Yes" ; elseecho "No"; // This code is contributed by anuj_67. ?> |
Yes
Recommended Posts:
- Minimum number of steps to convert a given matrix into Diagonally Dominant Matrix
- Minimum number of steps to convert a given matrix into Upper Hessenberg matrix
- Minimum steps required to convert the matrix into lower hessenberg matrix
- Check if matrix can be converted to another matrix by transposing square sub-matrices
- Circular Matrix (Construct a matrix with numbers 1 to m*n in spiral way)
- Program to check diagonal matrix and scalar matrix
- Check if it is possible to make the given matrix increasing matrix or not
- Program to check if a matrix is Binary matrix or not
- 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
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j
- 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
- Matrix sum except one item
- Sum of GCDs of each row of the given 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.



