Javascript Program to check if a matrix is symmetric Last Updated : 10 Sep, 2024 Summarize Comments Improve Suggest changes Share 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 : NoA Simple solution is to do following. 1) Create transpose of given matrix. 2) Check if transpose and given matrices are same or not. JavaScript // Simple javascript code for check // a matrix is symmetric or not. let MAX = 100; // Fills transpose of mat[N][N] in tr[N][N] function transpose(mat, tr, N) { for (let i = 0; i < N; i++) for (let j = 0; j < N; j++) tr[i][j] = mat[j][i]; } // Returns true if mat[N][N] is // symmetric, else false function isSymmetric(mat, N) { let tr = new Array(N); for (let i = 0; i < N; i++) { tr[i] = new Array(MAX); } transpose(mat, tr, N); for (let i = 0; i < N; i++) for (let j = 0; j < N; j++) if (mat[i][j] != tr[i][j]) return false; return true; } // Driver code let mat = [[1, 3, 5], [3, 2, 4], [5, 4, 1]]; if (isSymmetric(mat, 3)) console.log("Yes"); else console.log("No"); // This code is contributed by decode2207 OutputYes Complexity Analysis: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]. JavaScript // Efficient Javascript code for check a matrix is symmetric or no let MAX = 100; // Returns true if mat[N][N] // is symmetric, else false function isSymmetric(mat, N) { for (let i = 0; i < N; i++) for (let j = 0; j < N; j++) if (mat[i][j] != mat[j][i]) return false; return true; } let mat = [[1, 3, 5], [3, 2, 4], [5, 4, 1]]; if (isSymmetric(mat, 3)) console.log("Yes"); else console.log("NO"); OutputYes Complexity Analysis:Time Complexity : O(N x N) Auxiliary Space : O(1) Comment More infoAdvertise with us Next Article Javascript Program to check if a matrix is symmetric K kartik Follow Improve Article Tags : JavaScript Similar Reads Javascript 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 Javascript Program to check Involutory Matrix Given a matrix, the task is to check matrix is an involutory matrix or not. Involutory Matrix: A matrix is said to be an involutory matrix if the matrix multiplied by itself returns the identity matrix. The involutory matrix is the matrix that is its inverse. The matrix A is said to be an involutory 2 min read Javascript Program to check idempotent matrix Given an N * N matrix and the task is to check matrix is an idempotent matrix or not.Idempotent matrix: A matrix is said to be an idempotent matrix if the matrix multiplied by itself returns the same matrix. The matrix M is said to be an idempotent matrix if and only if M * M = M. In an idempotent m 2 min read Javascript Program for Diagonally Dominant Matrix In mathematics, a square matrix is said to be diagonally dominant if for every row of the matrix, the magnitude of the diagonal entry in a row is larger than or equal to the sum of the magnitudes of all the other (non-diagonal) entries in that row. More precisely, the matrix A is diagonally dominant 2 min read Javascript 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 Up 2 min read Like