Transpose of Matrix | Practice | GeeksforGeeks

Write a program to find the transpose of a square matrix of size N*N. Transpose of a matrix is obtained by changing rows to columns and columns to rows.

Example 1:
Input:
N = 4
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}} 

Example 2:
Input:
N = 2
mat[][] = {{1, 2},
           {-9, -2}}
Output:
{{1, -9}, 
 {2, -2}}


Your Task:
You dont need to read input or print anything. Complete the function transpose() which takes matrix[][] and N as input parameter and finds the transpose of the input matrix. You need to do this in-place. That is you need to update the original matrix with the transpose. 

Expected Time Complexity: O(N * N)
Expected Auxiliary Space: O(1)

Constraints:
1 <= N <= 100
-103 <= mat[i][j] <= 103
0
jaatgfg113 weeks ago
c++ ezz  pzz
 
    void transpose(vector<vector<int> >& matrix, int n)   {        for(int i=0;i<n;i++){           for(int j=0;j<i;j++){               swap(matrix[i][j],matrix[j][i]);           }       }   }
0
saiakhilpodduturi1 month ago
Transpose of Matrix (Java Solution)
class Solution
{
    //Function to find transpose of a matrix.
    static void transpose(int matrix[][], int n)
    {
        // code here
        for (int i=0; i<n-1; i++)
        {
            for (int j=i+1; j<n; j++)
            {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
    }
}
0
aakasshuit2 months ago
//Java Solution

        for(int i =0 ; i<n ; i++){
            for(int j =i ; j<n ; j++){
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
0
sunghunet2 months ago
# Python Version
def transpose(self, m, n):
    for i in range(n):
        for j in range(i+1, n):
            m[i][j], m[j][i] = m[j][i], m[i][j] 
 
+1
sunghunet2 months ago
We only need to access the range of an upper triangular matrix or a lower triangular matrix. There is no need to iterate over every number of elements in the matrix.
Just put j=i+1. 
void transpose(std::vector<std::vector<int> >& matrix, int n) {
    for(int i=0;i<n;i++)
        for(int j=i+1;j<n;j++)
            std::swap(matrix[i][j], matrix[j][i]);
}

 
0
lakshyanawandhar4573 months ago
// { Driver Code Starts#include <bits/stdc++.h> using namespace std;
// } Driver Code Endsclass Solution{      public:     //Function to find transpose of a matrix.   void transpose(vector<vector<int> >& m, int n)   {        // code here        for(int i=0;i<n;++i){           for(int j=i;j<n;++j){               swap(m[i][j],m[j][i]);           }       }   }};
// { Driver Code Starts.
int main() {   int t;   cin>>t;      while(t--)    {       int n;       cin>>n;       vector<vector<int> > matrix(n);
       for(int i=0; i<n; i++)       {           matrix[i].assign(n, 0);           for( int j=0; j<n; j++)           {               cin>>matrix[i][j];           }       }
       Solution ob;       ob.transpose(matrix,n);       for (int i = 0; i < n; ++i)           for (int j = 0; j < n; ++j)               cout<<matrix[i][j]<<" ";       cout<<endl;   }   return 0;}  // } Driver Code Ends
0
dcvog35f4ne2xjgysnru01jdwe9xsoscyyq121kj3 months ago
void transpose(vector<vector<int> >& matrix, int n)   {        for(int i=0;i<n;i++){           for(int j=i+1;j<matrix[i].size();j++){               if(i==j){                   continue;               }               swap(matrix[i][j],matrix[j][i]);           }       }   }
0
rko163 months ago
Total Time Taken:
0.1/1.1
 void transpose(vector<vector<int> >& matrix, int n)   {        for(int i=0;i<n;i++)           for(int j=i+1;j<n;j++)           swap(matrix[i][j],matrix[j][i]);   }
+1
gauravg770613 months ago
 void transpose(vector<vector<int> >& matrix, int n)   {        // code here        for(int i=0;i<n;i++){           for(int j=0;j<i;j++){               int temp=matrix[i][j];               matrix[i][j]=matrix[j][i];               matrix[j][i]=temp;           }       }        }
0
mindy3 months ago
 void transpose(vector<vector<int> >& matrix, int n)
    { 
   for(int i=0; i<n; i++)
 {
  for(int j=i; j<n; j++)
  {
    if(i!=j)  swap(matrix[i][j],matrix[j][i]);
  }
  }
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.