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
```

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `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

 ` `

Output :

```Yes
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Improved By : vt_m, manishshaw1

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.