C++ 2-D Arrays | Set-1 | Practice | GeeksforGeeks

Given an 2-d array ofintegers havingN*Nelements, print the transpose of the array.
Example 1:
Input : arr[] = {{1,2,3}, {4,5,6}, {7,8,9}}         and N = 3Output : 1 4 7 2 5 8 3 6 9Explanation:1 2 3       1 4 74 5 6 ----> 2 5 87 8 9       3 6 9Transpose of array.
Example 2:
Input : arr[ ] = {{1, 2}, {1, 2}} and N = 2 Output :  1 1 2 2
Your Task:This is a function problem. The input is already taken care of by the driver code. You only need to complete the function transpose() that takes a two-dimensionarray (arr), sizeOfArray (n), and return the transpose of the array. The driver code takes care of the printing.
Expected Time Complexity:O(N2).Expected Auxiliary Space:O(1).
Constraints:1 ≤ N ≤ 1000 ≤ A[i] ≤ 1000
0
harshscode1 week ago
vector<vector<int>> b(n,vector<int>(n));      for(int i=0;i<n;i++)   {       for(int j=0;j<n;j++)       {           b[j][i]=a[i][j];       }   }   return b;
+1
rishi1731 week ago
vector<vector<int>> transpose(int a[][M], int n){   vector<vector<int>> b(n, vector<int>(n,0));//created a 2D vector and after   //that created another vector of size n with all values as 0.   for(int i=0;i<n;i++)   {           for(int j=0;j<n;j++)           {               b[i][j]=a[j][i];           }   }   return b;}
 
+2
0niharika22 months ago
vector<vector<int>> b(n,vector<int>(n));
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            b[j][i]=a[i][j];
        }
    }
    return b;
0
mohammadtanveer75402 months ago
vector<vector<int>> transpose(int a[][M], int n)
{
    // Code here
    vector< vector<int> > transpose;
    
    for(int i=0;i<M;i++)
    {
        vector<int> temp;
        
        for(int j=0;j<n;j++)
        {
            int x; 
            x = a[j][i];
            
            temp.push_back(x);
        }
        
        transpose.push_back(temp);
    }
    
    return transpose;
    
}

+4
naimatofficial104 months ago
// Transport of 2D - array            if (n==0)           {               return {{}};           }           int row = n, col = n;            vector<vector<int>> A_Transport(n, vector<int>(row));           for (int row = 0; row < n; row++)           {               for (int col = 0; col < n; col++)               {                   A_Transport[col][row] = a[row][col];               }           }           // print the Transport of A            return A_Transport;        
0
mdseraj960885 months ago
vector<vector<int>> transpose(int a[][M], int n){   //my code is here   if(n==0){       return {{}};   }   int row=n;   int col=n;   vector<vector<int>> v(n,vector<int>(row));   for(int i=0;i<row;i++){       for(int j=0;j<col;j++){          v[j][i]=a[i][j];       }   }     return v;   }
+1
badgujarsachin835 months ago
vector<vector<int>> transpose(int a[][M], int n)
{
    // Code here]
    if(n==0){
        return {{}};
    }
    int row=n;
    int col=n;
    vector<vector<int>> v(n,vector<int>(row));
    for(int i=0;i<row;i++){
        for(int j=0;j<col;j++){
           v[j][i]=a[i][j];
        }
    }
   
    return v;
    
}

-1
ankita12apr6 months ago
 int rows = n;       if (rows == 0) return {{}};       int cols = n;       vector<vector<int>> r(cols, vector<int>(rows));               for (int i = 0; i < rows; ++ i) {           for (int j = 0; j < cols; ++ j) {               r[j][i] = a[i][j];           }       }               return r;
-2
pankajkumarravi6 months ago
************************ Correct java logic *************
 
int[][] transpose(int a[][], int n){    // Code here    int [][] transpose = new int[n][n];   for (int i=0;i<n;i++){       for(int j=0;j<n;j++){               transpose[j][i] = a[i][j];       }   }   return transpose;}
-1
sparshgoelt267 months ago
Please change the test case on Compile and Run.
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.