# JavaScript Program to Check horizontal and vertical symmetry in binary matrix

A binary Matrix is a 2-D array that consists of one and zero only as the elements in each cell. Horizontal symmetry of a binary matrix means if the first row is the same as the last row, the second row is the same as the second last row, and so on. Similarly, vertical symmetry means if the first and last column, second and second last column, and so on are the same. In this problem, we are given a matrix and we will find if horizontal and vertical symmetry is present in it or not.

### Input

1 0 1
0 0 0
1 0 1


### Output

Both, horizontal and vertical symmetry is present.


Explanation −The first row and last row are the same which means there is horizontal symmetry. Similarly, first and last columns are the same leading to the vertical symmetry.

### Input

1 0 1
0 0 0
1 1 0


### Output

None of the symmetry is present.


Explanation − The first row is not equal to the last row and the first column is not equal to the last column.

## Approach

We have seen the examples to get an idea about the given problem, now let us see steps to implement the code −

• First we will define a function to check the horizontal symmetry of the given matrix. This function will take a single parameter that is the given matrix and will return if the current matrix is horizontal symmetric or not.

• We will traverse over the matrix and for each row we will compare to the row present on the other side of the imaginary line passing through the middle of the matrix and at the same distance the as current one.

• We will define a function to check the vertical symmetry of the given matrix. This function will take a single parameter that is the given matrix.

• We will traverse over the matrix and for each column we will compare to the column present on the other side of the imaginary line passing through the middle of the matrix and at the same distance as the current one.

• We will call both functions and based on the return value we will print the result.

### Example

// function to check horizontal symmetry
function horizontalSymm(mat){
var rows = mat.length;
var cols = mat.length;
for(var i = 0; i< rows/2; i++){
for(var j = 0;j<cols; j++){
if(mat[i][j] != mat[rows-i-1][j]){
return false;
}
}
}
return true;
}

// function to check vertical symmetry
function verticalSymm(mat){
var rows = mat.length;
var cols = mat.length;
for(var i = 0; i< cols/2; i++){
for(var j = 0;j<rows; j++){
if(mat[j][i] != mat[j][cols-i-1]){
return false;
}
}
}
return true;
}

// function to check the symmetry of the given matrix
function check(mat){
var horSymm = horizontalSymm(mat);
var varSymm = verticalSymm(mat);
if(horSymm && varSymm){
console.log("Both, horizontal and vertical symmetries are present in the given matrix");
}
else if(horSymm){
console.log("The given binary matrix is only horizontally symmetric");
}
else if(varSymm){
console.log("The given binary matrix is only vertically symmetric");
}
else{
console.log("The given binary matrix is neither horizontally symmetric nor vertically symmetric");
}
}

// defining the given matrix
var mat = [[1, 0, 1], [0, 0, 0], [1, 0, 1]];
console.log("The given matrix is: ")
console.log(mat);
check(mat);

// defining the given matrix
var mat = [[1, 0, 1], [0, 0, 0], [1, 1, 0]];
console.log("The given matrix is: ")
console.log(mat);
check(mat);


### Output

The given matrix is:
[ [ 1, 0, 1 ], [ 0, 0, 0 ], [ 1, 0, 1 ] ]
Both, horizontal and vertical symmetries are present in the given matrix
The given matrix is:
[ [ 1, 0, 1 ], [ 0, 0, 0 ], [ 1, 1, 0 ] ]
The given binary matrix is neither horizontally symmetric nor vertically symmetric


### Time and Space Complexity

The time complexity of the above code is O(N*M) where N is the number of rows of the given matrix and M is the number of columns of the given matrix. We are traversing the complete matrix two times one for the horizontal symmetry and another for the vertical symmetry.

The Space complexity of the above code is O(1), as we are not using any extra space.

## Conclusion

In this tutorial, we have implemented a JavaScript program to find a given matrix is that current matrix is horizontally or vertically symmetric or not. Horizontal symmetry of a binary matrix means if the first row is the same as the last row, the second row is exactly the same as the second last row, and so on. Similarly, vertical symmetry means if the first and last column, second and second last column, and so on are the same. We have implemented a program with O(N*M) time complexity and O(1) space complexity.

Updated on: 18-Apr-2023

196 Views 