C++ Program to check if a matrix is symmetric Last Updated : 11 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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.  C++ // Simple c++ code for check a matrix is // symmetric or not. #include <iostream> using namespace std; const int MAX = 100; // Fills transpose of mat[N][N] in tr[N][N] void transpose(int mat[][MAX], int tr[][MAX], int N) { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) tr[i][j] = mat[j][i]; } // Returns true if mat[N][N] is symmetric, else false bool isSymmetric(int mat[][MAX], int N) { int tr[N][MAX]; transpose(mat, tr, N); for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) if (mat[i][j] != tr[i][j]) return false; return true; } // Driver code int main() { int mat[][MAX] = { { 1, 3, 5 }, { 3, 2, 4 }, { 5, 4, 1 } }; if (isSymmetric(mat, 3)) cout << "Yes"; else cout << "No"; return 0; } 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].  C++ // Efficient c++ code for check a matrix is // symmetric or not. #include <iostream> using namespace std; const int MAX = 100; // Returns true if mat[N][N] is symmetric, else false bool isSymmetric(int mat[][MAX], int N) { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) if (mat[i][j] != mat[j][i]) return false; return true; } // Driver code int main() { int mat[][MAX] = { { 1, 3, 5 }, { 3, 2, 4 }, { 5, 4, 1 } }; if (isSymmetric(mat, 3)) cout << "Yes"; else cout << "No"; return 0; } Output: Yes Time Complexity : O(N x N) Auxiliary Space : O(1) Please refer complete article on Program to check if a matrix is symmetric for more details! Comment More infoAdvertise with us Next Article C++ Program to check if a matrix is symmetric K kartik Follow Improve Article Tags : C++ Practice Tags : CPP Similar Reads How to Check if Two Vectors are Equal in C++? Two vectors are said to equal if all the elements at every index of vectors is equal. In this article, we will learn how to check if two vectors are equal or not in C++.The simplest method to check if two vector are equal is by using comparison operator (==). Letâs take a look at an example:C++#incl 3 min read C++ Program to Check if a given matrix is sparse or not A matrix is a two-dimensional data object having m rows and n columns, therefore a total of m*n values. If most of the values of a matrix are 0 then we say that the matrix is sparse. Consider a definition of Sparse where a matrix is considered sparse if the number of 0s is more than half of the elem 2 min read C++ Program to check if matrix is upper triangular Given a square matrix and the task is to check the matrix is in upper triangular form or not. A square matrix is called upper triangular if all the entries below the main diagonal are zero. Examples: Input : mat[4][4] = {{1, 3, 5, 3}, {0, 4, 6, 2}, {0, 0, 2, 5}, {0, 0, 0, 6}}; Output : Matrix is in 2 min read C++ Program to Check horizontal and vertical symmetry in binary matrix Given a 2D binary matrix of N rows and M columns. The task is to check whether the matrix is horizontal symmetric, vertical symmetric, or both. The matrix is said to be horizontal symmetric 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. An 4 min read C++ Program to check if matrix is lower triangular Given a square matrix and the task is to check the matrix is in lower triangular form or not. A square matrix is called lower triangular if all the entries above the main diagonal are zero. Examples: Input : mat[4][4] = {{1, 0, 0, 0}, {1, 4, 0, 0}, {4, 6, 2, 0}, {0, 4, 7, 6}}; Output : Matrix is in 2 min read Like