Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: 17 Oct, 2020

Matrix Is Symmetric

Easy
Asked in companies
Symphony Talent, LLCJosh Technology GroupPersistent Systems

Problem statement

You are given a square matrix, return true if the matrix is symmetric otherwise return false.

A symmetric matrix is that matrix whose transpose is equal to the matrix itself.

Example of symmetric matrix :

Symmetric Example

Input Format:
The first line contains an Integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.

The first line of each test case contains the size of the square matrix 'N'.

The second line of each test case contains the 'N' * 'N' Integers separated by a single space (the matrix is entered row-wise)     
Output format :
For each test case/query, print whether the given matrix is symmetric or not.

Output for every test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N  <= 10^2
-10^9 <= data <= 10^9

Where 'data' denotes the element in the given 'matrix'.

Time Limit: 1 sec

Approaches

01 Approach

  • Create another matrix ‘TRANSPOSE’ of the same size initially having all zeros.
  • Now replace ‘TRANSPOSE’ (i,j) with ‘MATRIX’ (j,i).
  • And now traverse both matrixes element by element if anywhere there is inequality return false, else continue.
  • In the end, return true as you traverse.