Transpose of a Matrix

Learn via video course
FREE
View all courses
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
Topics Covered

Problem Statement

Given a 2D matrix having nn rows and mm 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.

transpose-of-matrix-example

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 n=2n=2 and m=3m=3, followed by n×m=2×3=6n \times m = 2 \times 3 = 6 numbers as input, i.e. 2 4 6 5 7 02\ 4\ 6\ -5\ 7\ 0.

Hence the matrix formed is

[246570]\begin{bmatrix} 2 & 4 \\ 6 & -5 \\ 7 & 0 \end{bmatrix}

Once the code transposes this matrix, i.e. it interchanges the columns as rows and vice versa, the output becomes

[267450]\begin{bmatrix} 2 & 6 & 7\\ 4 & -5 & 0 \end{bmatrix}

This is the transpose of the given matrix.

Example 2:

In this example, first the user enters the value of n=3n=3 and m=3m=3, followed by n×m=3×3=9n \times m = 3 \times 3 = 9 numbers as input, i.e. 1 2 3 4 5 6 7 8 91\ 2\ 3\ 4\ 5\ 6\ 7\ 8\ 9.

Hence the matrix formed is [123456789]\begin{bmatrix} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end{bmatrix}

Once the code transposes this matrix, i.e. it interchanges the columns as rows and vice versa, the output becomes

[147258369]\begin{bmatrix} 1 & 4 & 7\\ 2 & 5 & 8\\ 3 & 6 & 9 \end{bmatrix}

This is the transpose of the given matrix.

Constraints

The following are the constraints assumed while we perform transpose of a matrix.

n==n == number of rows m==m == number of columns 1<=m,n<=10001 <= m, n <= 1000 1<=mn<=1051 <= m * n <= 10^5 109<=matrix[i][j]<=109-10^9 <= matrix[i][j] <= 10^9

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Approach 1 - Using Functions - For Loop

Algorithm

  1. Take input of nn and mm
  2. take input in matrix array of size n×mn \times m.
  3. create another transpose_matrix array of size m×nm \times n transpose of the input matrix.
  4. Loop through the matrix array and convert its rows into columns of transpose_matrix array using transpose_matrix[ i ][ j ] = matrix[ j ][ i ];
  5. 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

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Output

Input

Output:

Time Complexity

The time complexity for transposing the matrix here will be O(n2)O(n^2) since we are using nested for loops to process the matrix array.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Space Complexity

The space complexity for transposing the matrix in this case will be O(n2)O(n^2) 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

  1. Take input of nn and mm
  2. take input in matrix array of size n×mn \times m.
  3. print the matrix array.
  4. loop for nn times a. In the iith loop, loop from i+1i+1 element to nn times and swap the matrix[i][j] element with matrix[j][i] element
  5. print matrix array that is now transposed.

Note: This algorithm will only work for square matrices having n×nn \times n dimension, i.e. m==nm==n

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 O(n2)O(n^2) 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 O(1)O(1) 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 O(n2)O(n^2) time complexity and O(n2)O(n^2) space complexity.
  • If we do not take extra space and transform the matix then it takes O(n2)O(n^2) time complexity and O(1)O(1) space complexity.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more