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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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"
else
echo "No";
  
// This code is contributed by anuj_67.
?>

chevron_right


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.