VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-to-check-if-a-matrix-is-symmetric/

⇱ Program to check if a matrix is symmetric - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to check if a matrix is symmetric

Last Updated : 18 Jul, 2022

A square matrix is said to be symmetric matrix if the transpose of the matrix is same as the given matrix. Symmetric matrix can be obtain by changing row to column and column to row.

Examples: 

Input : 1 2 3
 2 1 4
 3 4 3
Output : Yes

Input : 3 5 8
 3 4 7
 8 5 3
Output : No

A Simple solution is to do following. 

  1. Create transpose of given matrix. 
  2. Check if transpose and given matrices are same or not,  

Implementation:


Output
Yes

Time Complexity : O(N x N) 
Auxiliary Space : O(N x N)

An Efficient solution to check a matrix is symmetric or not is to compare matrix elements without creating a transpose. We basically need to compare mat[i][j] with mat[j][i].  

Implementation:


Output
Yes

Time Complexity : O(N x N) 
Auxiliary Space : O(1)

This article is contributed by Dharmendra kumar.  

Comment
Article Tags:
Article Tags: