Bisymmetric matrix
Last Updated :
29 Sep, 2022
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 it 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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 100
bool checkBisymmetric( int m[][MAX],
int n)
{
for ( int i = 0; i < n; i++)
for ( int j = 0; j < i; j++)
if (m[i][j] != m[j][i])
return false ;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n - i; j++)
if (m[i][j] != m[n - j - 1]
[n - i - 1])
return false ;
return true ;
}
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
import java.io.*;
class GFG {
static int MAX = 100 ;
static boolean checkBisymmetric( int m[][],
int n)
{
for ( int i = 0 ; i < n; i++)
for ( int j = 0 ; j < i; j++)
if (m[i][j] != m[j][i])
return false ;
for ( int i = 0 ; i < n; i++)
for ( int j = 0 ; j < n - i; j++)
if (m[i][j] != m[n - j - 1 ]
[n - i - 1 ])
return false ;
return true ;
}
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" );
}
}
|
Python3
def checkBisymmetric(m, n) :
for i in range ( 0 , n) :
for j in range ( 0 , i) :
if (m[i][j] ! = m[j][i]) :
return false
for i in range ( 0 , n) :
for j in range ( 0 , n - i) :
if (m[i][j] ! =
m[n - j - 1 ][n - i - 1 ]) :
return False
return True ;
n = 3 ;
m = [[ 1 , 2 , 3 ],
[ 2 , 5 , 2 ],
[ 3 , 2 , 1 ]]
if (checkBisymmetric(m, n)) :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool checkBisymmetric( int [,]m,
int n)
{
for ( int i = 0; i < n; i++)
for ( int j = 0; j < i; j++)
if (m[i,j] != m[j,i])
return false ;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n - i; j++)
if (m[i,j] != m[n - j - 1,
n - i - 1])
return false ;
return true ;
}
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" );
}
}
|
PHP
<?php
function checkBisymmetric( $m , $n )
{
for ( $i = 0; $i < $n ; $i ++)
for ( $j = 0; $j < $i ; $j ++)
if ( $m [ $i ][ $j ] != $m [ $j ][ $i ])
return false;
for ( $i = 0; $i < $n ; $i ++)
for ( $j = 0; $j < $n - $i ; $j ++)
if ( $m [ $i ][ $j ] != $m [ $n - $j - 1]
[ $n - $i - 1])
return false;
return true;
}
$n = 3;
$m = array ( array ( 1, 2, 3 ),
array ( 2, 5, 2 ),
array ( 3, 2, 1 ));
if (checkBisymmetric( $m , $n ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
let MAX = 100;
function checkBisymmetric(m, n)
{
for (let i = 0; i < n; i++)
for (let j = 0; j < i; j++)
if (m[i][j] != m[j][i])
return false ;
for (let i = 0; i < n; i++)
for (let j = 0; j < n - i; j++)
if (m[i][j] != m[n - j - 1]
[n - i - 1])
return false ;
return true ;
}
let n = 3;
let m = [ [ 1, 2, 3 ],
[ 2, 5, 2 ],
[ 3, 2, 1 ] ];
if (checkBisymmetric(m, n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...