## 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:

- Declare variables for rows and columns of the given matrix.
- Initialize the matrix with the elements and print the original matrix.
- Declare the second matrix for storing the transposed elements.
- Store the elements in the transpose matrix by interchanging the elements at the i
^{th}and j^{th}indices of the rows and columns. - 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.