1.
Introduction
1.1.
Problem Statement
1.2.
Sample Example
2.
Brute Force Approach
2.1.
Pseudocode
2.2.
Implementation in Java
2.3.
Complexity Analysis
3.
Optimized Approach
3.1.
Implementation in Java
3.2.
Complexity Analysis
4.
4.1.
What are in-place operations?
4.2.
Which properties are followed by a transpose matrix?
4.3.
What is the result when we take the transpose of a transpose matrix?
5.
Conclusion
Last Updated: Mar 27, 2024

# Java program to find transpose of a matrix

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

## Introduction

In this blog, we will look at the approach to find the transpose of a matrix.

The transpose of a matrix is found by interchanging the elements of the rows and columns. It is denoted using the letter â€śTâ€ť in the superscript of the given matrix and if initial dimensions of a matrix were (mxn) then, after the transpose the new dimensions of the transpose matrix become (nxm).

### Problem Statement

Given: A matrix of mxn dimensions.

Problem: Find the transpose of a matrix and display the transpose matrix.

### Sample Example

Let us look at the example of the following matrix to learn how to find the transpose of a matrix.

This matrix has the dimensions of 4x3, where we have 4 rows and 3 columns. The transpose of the matrix is defined by the matrix where the elements in the rows and columns are interchanged. Therefore, the transpose of the above matrix will be given as follows:

After the interchange of row and column elements, the new dimensions of the matrix are 3x4.

## Brute Force Approach

In this approach, we will use two matrices, one is the given matrix and after iterating and interchanging values in the rows and columns we will store the element values in the second matrix.

### Pseudocode

The pseudocode to find the transpose of a matrix is as follows:

1. Declare variables for rows and columns of the given matrix.
2. Initialize the matrix with the elements and print the original matrix.
3. Declare the second matrix for storing the transposed elements.
4. Store the elements in the transpose matrix by interchanging the elements at the ith and jth indices of the rows and columns.
5. Print the second matrix, i.e., the transpose matrix.

### Implementation in Java

/*Program to find the transpose of a matrix*/
import java.util.*;

public class Main
{
public static void main(String []args)
{
Scanner s = new Scanner(System.in);
int m,n;
System.out.println("Enter the number of rows: \n");
m=s.nextInt();

System.out.println("Enter the number of column: \n");
n=s.nextInt();

int a1[][]=new int[m][n];
System.out.println("Enter the elements of the matrix: ");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a1[i][j]=s.nextInt();
}
}

//Print the given matrix
System.out.println("The given matrix is: ");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(a1[i][j]+" ");
}
System.out.println("");
}

int a2[][]=new int[n][m];
// Find the transpose of the matrix
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
a2[j][i]=a1[i][j];
}
}

System.out.println("The transpose matrix is:");
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(a2[i][j]+" ");
}
System.out.println("");
}

}
}

Output:

Enter the number of rows: 4
Enter the number of column: 3
Enter the elements of the matrix: 2 9 3 13 11 17 3 6 15 4 13 1

The given matrix is:
2   9   3
13 11 17
3   6   15
4   13  1

The transpose matrix is:
2 13  3  4
9 11  6  13
3 17 15  1

Practice it on online java compiler for better understanding.

### Complexity Analysis

Time Complexity: The time complexity of the program is O(mxn), where m and n are the dimensions of the matrix as we traverse through the complete matrix mxn times.

Space Complexity: The space complexity is O(mxn) because we need mxn space to store the (mxn) elements of the matrix.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Optimized Approach

In this approach, instead of using another matrix to store the transpose of a matrix, we will interchange the elements of the rows and columns in-place which will reduce the space complexity for the program. This approach can only be used in case of square matrices with nxn dimension because the number of rows of a square matrix is equal to the number of columns, and because of this the size of the original and the transpose matrix is the same.

### Implementation in Java

import java.util.*;

public class Main
{
public static void main (String[] args)
{
int M[][] = {{2,9,3},
{13,11,17},
{3,6,15},
{4,13,1}};
int m = 4;
int n = 3;

for(int i=0;i<m;i++){
for(int j=i;j<n;j++){

int temp = M[i][j];
M[i][j] = M[j][i];
M[j][i] = temp;

}
}

for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
System.out.print(M[i][j] + "    ");
}
System.out.println();
}

}
}

Output:

2 13  3  4
9 11  6  13
3 17 15  1

### Complexity Analysis

Time Complexity: The time complexity remains the same, i.e., O(mxn) where n is the number of rows and columns of the matrix and we traverse over (mxn) elements.

Space Complexity: The space complexity is reduced to O(1), because we perform operations in-place.

Also check out Addition of Two Numbers in Java here.

Also see, Duck Number in Java

### What are in-place operations?

These are the operations which can be performed without using temporary variables for storing temporary values and the changes in the variables are directly made.

### Which properties are followed by a transpose matrix?

The properties of a transpose matrix are multiplication by a constant, addition property and multiplication property of transpose.

### What is the result when we take the transpose of a transpose matrix?

The result is the matrix itself when we take the transpose of a transpose matrix, because (Aâ€™)â€™ = A.

## Conclusion

This article discussed the approach to find the transpose of a matrix in Java. We have also implemented the optimized approach for finding the transpose of a matrix using in-place operations.

Refer to our guided paths on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must have a look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass