Transpose of Matrix solution


Transpose of Matrix

Difficulty: EasyAccuracy: 66.5%Submissions: 118K+Points: 2Average Time: 20m

You are given a square matrix of size n x n. Your task is to find the transpose of the given matrix.
The transpose of a matrix is obtained by converting all the rows to columns and all the columns to rows.

Examples :

Input: mat[][] = [[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4]] Output: [[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]]
Explanation: Converting rows into columns and columns into rows.
Input: mat[][] =  [[1, 2],
[9, -2]] Output: [[1, 9],
[2, -2]]
Explanation: C
onverting rows into columns and columns into rows.

Constraints:
1 ≤ n ≤ 103
-109 ≤ mat[i][j] ≤109

Expected Complexities
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Topic Tags
Related Interview Experiences
Related Articles

Comments

Popular Posts