Matrix: Check if the matrix is Bisymmetric matrix or not

Problem Statement:

You are given a matrix, you need to check if the matrix is Bi-symmetric matrix or not.

A square matrix is considered as Bi-symmetric matrix if the matrix is symmetric if both of its main diagonals are symmetric.

Example:

Input: 

n = 3
m[] [] = { {1, 2, 3},
		   {2, 3, 2},
		   {3, 2, 1} };

Output: Yes

Solution Explanation:

Solution is vary simple.

We need to check if the matrix is symmetric of its main diagonals.

For the solution, we need to check for below conditions:

Check from Top Left to Bottom Right meaning m[i][j] is equal to m[j][i].

Check from Top Right to Bottom Left meaning m[i][j] is equal to m[n-j-1][n-i-1].

If both of the above conditions are true, then the given matrix is a Bisymmetric matrix.

Code Solution

#include <iostream>
#include <vector>
using namespace std;

bool solution(int m[][10], 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[][10] = { { 1, 2, 3 },
                    { 2, 5, 2 },
                    { 3, 2, 1 } };

    if(solution(m, n)){
        cout << "Yes";
    } else{
        cout << "No";
    }
    return 0;
}

Output

Yes
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *