C++ Matrix Rotation by 180 degree | Practice | GeeksforGeeks

Given a square matrix of size N X N, turn it by 180 degrees in anticlockwise direction without using extra memory.
 
Example 1:
Input: N = 4, 
matrix = {{1, 2, 3, 4}, 
          {5, 6, 7 ,8}, 
          {9, 10, 11, 12},
          {13, 14, 15, 16}}
Output: {{16, 15, 14, 13}, 
         {12, 11, 10, 9}, 
         {8, 7, 6, 5}, 
         {4, 3, 2, 1}}

Example 2:
Input: N = 2,
matrix = {{1, 2},
          {3, 4}}
Output: {{4, 3}, 
         {2, 1}}

 
Your Task:
You don't need to read or print anything. Your task is to complete the function rotate() which takes matrix as input parameter and rotate the matrix by 180 degree wihtout using extraa memory. You have to rotate the matrix in-place which means you have to modify the input matrix directly.
 
Expected Time Complexity: O(N2)
Expected Space Complexity: O(1)
 
Constraints:
1 ≤ N ≤ 500
+2
badgujarsachin831 month ago
    void rotate(vector<vector<int> >& matrix) {
        // Code here
        int n=matrix.size();
        for(int i=0;i<n;i++){
            for(int j=i;j<n;j++){
                swap(matrix[i][j],matrix[j][i]);
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n-i;j++){
                swap(matrix[i][j],matrix[n-1-j][n-1-i]);
            }
        }
0
chessnoobdj2 months ago
C++ inplace, take transpose twice
void rotate(vector<vector<int> >& A) {
        int n = A.size();
        for(int i=0; i<n; i++)
            for(int j=i; j<n; j++)
                swap(A[i][j], A[j][i]);
        for(int i=0; i<n; i++)
            for(int j=0; j<n-i; j++)
                swap(A[i][j], A[n-1-j][n-1-i]);
    }
+1
nyngupta003 months ago
 
   void rotate(vector<vector<int> >& matrix)    {     int n=matrix.size();         for(int i=0;i<n/2;i++)     for(int j=0;j<n;j++)     {         int t=matrix[i][j];         matrix[i][j]=matrix[abs(i-(n-1))][abs(j-(n-1))];         matrix[abs(i-(n-1))][abs(j-(n-1))]=t;     }     if(n%2!=0)     {         for(int i=0;i<n/2;i++)         {          int t=matrix[n/2][i];          matrix[n/2][i]=matrix[n/2][abs(i-(n-1))];          matrix[n/2][abs(i-(n-1))]=t;           }                  }   }
 
0
shubham9063 months ago
 
//User function Template for Java
class Solution{   public void  rotate(int[][] matrix)   {       // code here       int l = matrix.length;       int arr[] = new int[l*l];       int count =0;       for(int i=0;i<l;i++)       {           for(int j=0;j<l;j++)           {               arr[count]=matrix[i][j];               count++;           }       }       count--;        for(int i=0;i<l;i++)       {           for(int j=0;j<l;j++)           {               matrix[i][j] = arr[count];               count--;           }       }   }}
+3
nitind3563 months ago
Simple C++ code:
void rotate(vector<vector<int> >& matrix)
{
	reverse(matrix.begin(),matrix.end());
    for(int i=0;i<matrix[0].size();i++){
    reverse(matrix[i].begin(),matrix[i].end());
    }
}
-1
aprameya0836 months ago
C++ code-
class Solution {
public:
    void rotate(vector<vector<int> >& matrix) {
        
        int t=2;
         int n=matrix.size();
        while(t>0){
           
    
     for(int i=0; i<n; i++){
        for(int j=i; j<n; j++){
            int temp1=matrix[i][j];
            matrix[i][j]=matrix[j][i];  
            matrix[j][i]=temp1;
        }
    }
    
    for(int i=0; i<n/2; i++){
        for(int j=0; j<n; j++){
            int temp2=matrix[i][j];
            matrix[i][j]=matrix[n-i-1][j];  
            matrix[n-i-1][j]=temp2;
        }
    }t--;
        }
    }
};
 
0
Imran Wahid8 months ago
Imran Wahid
Easy C++ solutionhttps://ide.geeksforgeeks.o... https://uploads.disquscdn.c...
0
Dhruv Bajoria9 months ago
Dhruv Bajoria
void rotatea(vector<vector<int> >& matrix){    int n=matrix.size();    for(int i=0;i<n;i++) reverse(matrix[i].begin(),matrix[i].end());="" for(int="" i="0;i&lt;n;i++)" {="" for(int="" j="0;j&lt;i;j++)" swap(matrix[i][j],matrix[j][i]);="" }="" }="" void="" rotate(vector<vector<int=""> >& matrix) {       rotatea(matrix);       rotatea(matrix);    }
0
Dhruv Bajoria
This comment was deleted.
0
ANKIT SINHA9 months ago
ANKIT SINHA
https://uploads.disquscdn.c...
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.