![]() |
VOOZH | about |
Given a 2D matrix mat[][], compute its transpose. The transpose of a matrix is formed by converting all rows of mat[][] into columns and all columns into rows.
Example:
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: The output is the transpose of the input matrix, where each row becomes a column. This rearranges the data so that vertical patterns in the original matrix become horizontal in the result.Input: mat[][] = [[1, 2],
[9, -2]]
Output: [[1, 9],
[2, -2]]
Explanation: The output is the transpose of the input matrix, where each row becomes a column. This rearranges the data so that vertical patterns in the original matrix become horizontal in the result.
Table of Content
The idea is to create a new matrix where rows become columns by swapping indices — element at position [i][j] in the original becomes [j][i] in the transposed matrix.
Stepby Step Implementations:
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
This approach works only for square matrices, where the number of rows is equal to the number of columns. It is called anin-place algorithmbecause it performs the transposition without using any extra space.
Step by Step Implementations:
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4