Transpose of a Matrix
![DSA Problem Solving for Interviews using Java](https://www.scaler.com/topics/images/jitender_punia.webp)
Problem Statement
Given a 2D matrix having rows and number of columns. Print a transpose of the given matrix.
The transpose of a matrix is a new matrix that is obtained by exchanging the rows and columns.
Example
Let us have a look at some examples on how to transpose a matrix.
Example1
Input:
Output:
Example2
Input:
Output:
Example Explanation
Let us have a look at some examples to understand how to transpose a matrix.
Example 1:
In this example, first the user enters the value of and , followed by numbers as input, i.e. .
Hence the matrix formed is
Once the code transposes this matrix, i.e. it interchanges the columns as rows and vice versa, the output becomes
This is the transpose of the given matrix.
Example 2:
In this example, first the user enters the value of and , followed by numbers as input, i.e. .
Hence the matrix formed is
Once the code transposes this matrix, i.e. it interchanges the columns as rows and vice versa, the output becomes
This is the transpose of the given matrix.
Constraints
The following are the constraints assumed while we perform transpose of a matrix.
number of rows number of columns
Approach 1 - Using Functions - For Loop
Algorithm
- Take input of and
- take input in matrix array of size .
- create another transpose_matrix array of size transpose of the input matrix.
- Loop through the matrix array and convert its rows into columns of transpose_matrix array using transpose_matrix[ i ][ j ] = matrix[ j ][ i ];
- print matrix array and transpose_matrix array which contains transpose of the matrix.
Code Implementation(in C++, java , python)
C++ implementation
Java implementation
python implementation
Output
Input
Output:
Time Complexity
The time complexity for transposing the matrix here will be since we are using nested for loops to process the matrix array.
Space Complexity
The space complexity for transposing the matrix in this case will be since we are using an extra matrix to store the result of the transpose operation.
Approach 2 - Without Using Function - Interchanging the Row and Column
Algorithm
- Take input of and
- take input in matrix array of size .
- print the matrix array.
- loop for times a. In the th loop, loop from element to times and swap the matrix[i][j] element with matrix[j][i] element
- print matrix array that is now transposed.
Note: This algorithm will only work for square matrices having dimension, i.e.
Code Implementation(in C++, java , python)
C++ Code Implementation
Java implementation
Python implementation
Output
Input:
Output:
Time Complexity
The time complexity for the tranpose of the matrix here will be since we are using nested for loops to process the matrix array.
Space Complexity
The space complexity for the transpose of the matrix in this case will be since no extra space has been taken.
Conclusion
- The transpose of a matrix is a new matrix that is obtained by exchanging the rows and columns.
- If we take extra space and transform the matix then it takes time complexity and space complexity.
- If we do not take extra space and transform the matix then it takes time complexity and space complexity.