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
maneshram20Premium4 hours ago
class Solution
{   
    public:  
    //Function to find transpose of a matrix.
    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]);
            }
        }
    }
};
0
surajpatwa15062 days ago
// code here         for(int i=0;i<matrix.length;i++){           for(int j=i+1;j<matrix[i].length;j++){               //swap (a[i][j],a[j][i])               int temp=matrix[i][j];               matrix[i][j]=matrix[j][i];               matrix[j][i]=temp;           }       }       
0
arnab_deb_214 days ago
void transpose(vector<vector<int> >& matrix, int n)   {        vector<vector<int> > v1;       for(int i=0;i<n;i++){           vector<int> v2;           for(int j=0;j<n;j++){               v2.push_back(matrix[j][i]);           }           v1.push_back(v2);       }       matrix.clear();       copy(v1.begin(),v1.end(),back_inserter(matrix));   }
0
sahillather0024 days ago
        for(int i=0;i<n;i++)
        for(int j=i+1;j<n;j++)       swap(matrix[i][j],matrix[j][i]);
0
sahillather002
This comment was deleted.
0
tjanhvi5606 days ago
C++ Solution:
 
void transpose(vector<vector<int> >& matrix, int n)   {        for(int i=0;i<n;i++){           for(int j=i;j<n;j++){               swap(matrix[i][j], matrix[j][i]);           }       }   }
0
ncclalit20202083252 weeks ago
void transpose(vector<vector<int> >& matrix, int n)   {        // code here        for(int i=0;i<n-1;i++) {           for(int j=i+1;j<n;j++) {               if(i!=j) {                   swap(matrix[i][j],matrix[j][i]);               }            }       }   }
0
shashikantshrivastava132 weeks ago
JAVA SOLUTION  
//User function Template for Java
class Solution{   //Function to find transpose of a matrix.   static void transpose(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
divakarkumar20002 weeks ago
// C++ 
void transpose(vector<vector<int> >& matrix, int n)
    { 
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(i<j)
                {
                    swap(matrix[i][j],matrix[j][i]);
                }
            }
        }
        
    }
+1
vi5hnu2 weeks ago
//simple and efficient
for(int i=0;i<n;i++)       for(int j=i;j<n;j++)           swap(A[i][j],A[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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.