Rotate by 90 degree | Practice | GeeksforGeeks

Given a square matrix[][] of size N x N. The task is to rotate it by 90 degrees in an anti-clockwise direction without using any extra space.
Example 1:
Input:
N = 3
matrix[][] = [[1 2 3],
              [4 5 6],
              [7 8 9]]
Output:
3 6 9 
2 5 8 
1 4 7

Your Task:
You only need to implement the given function rotate(). Do not read input, instead use the arguments given in the function. 
Expected Time Complexity: O(N*N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 50
1 <= matrix[][] <= 100
 
+1
21ceuos083in 2 hours
 
//C++ CODE
void rotate(vector<vector<int> >& matrix){   int n = matrix.size();   vector<vector<int>> v{matrix};      for(int i = 0 ; i < n ; i++)   {       for(int j = 0 ; j < n ; j++)       {           v[i][j] = matrix[j][n-1-i];       }   }    for(int i = 0 ; i < n ; i++)   {       for(int j = 0 ; j < n ; j++)       {           matrix[i][j] = v[i][j];       }   }   }
0
tanashah1 day ago
its 1 point question but you need to change approach
https://practice.geeksforgeeks.org/problems/rotate-by-90-degree-1587115621/1/?page=1&difficulty[]=-1&difficulty[]=0&difficulty[]=1&difficulty[]=2&status[]=unsolved&category[]=Matrix&sortBy=submissions# 
 
+2
bhardwajji6 days ago
void rotate(vector<vector<int> >& mat)
{
    // step1 transpose
    // step2 interchange row from up and down 
    
    int n = mat.size();
    int m = mat[0].size();
    
    for(int i=0 ;i<n;i++){
        for(int j=0 ;j<i;j++){
            swap(mat[i][j],mat[j][i]);
        }
    }
    
    for(int i=0;i<n/2;i++){
        for(int j=0;j<m;j++){
        swap(mat[i][j], mat[n-i-1][j]);
        }
    }
    
}
0
illiyazzr2 weeks ago
Easy C++ Solution in within main() function:--
 
int main(){  int r=3,c=3;  int mat[r][c] = {{1,2,3},                          {4,5,6},                          {7,8,9}};  cout<<"Input matrix "<<endl<<endl;  for(int i=0;i<r;i++){     for(int j=0;j<c;j++){        cout<<mat[i][j]<<" ";     }     cout<<endl;  }  cout<<endl;  int transpose[r][c];    for(int i=0;i<r;i++){     for(int j=0;j<c;j++){        transpose[i][j] = mat[j][i];     }  }  cout<<"Transpose of the matrix "<<endl<<endl;  for(int i=0;i<r;i++){     for(int j=0;j<c;j++){        cout<<transpose[i][j]<<" ";     }     cout<<endl;  }  cout<<endl;  cout<<"Matrix rotated by 90 deg anti-clockwise"<<endl<<endl;    int start = r-1;  int end = 0;  while(start>=end){          for(int i=0;i<c;i++){                cout<<transpose[start][i]<<" ";     }     start--;     cout<<endl;  }}
 
0
rashmit pankhania3 weeks ago
guys simplest c++ solution 0.1 secs o(n**2) tc and o(1) sc
idea is to rotate four corners of square(in inner loop) anticlockwise(given) 
 
and go to inside of matrix from outside in each iteration of outer loop
 
simple :)
void rotate(int n,int a[][n])
{
   int tl,tr,bl,br;
   for(int i=0;i<n;i++){
    for(int j=i;j<n-i-1;j++){
     tl=a[i][j]; // top left
     tr=a[j][n-i-1]; // top right
     br=a[n-i-1][n-j-1]; // bottom right
     bl=a[n-j-1][i]; // bottom left
     
     a[i][j] = tr; // tl becomes tr
     a[j][n-i-1] = br; // tr becomes br
     a[n-i-1][n-j-1] = bl; // br becomes bl
     a[n-j-1][i]=tl; // bl becomes tl
    }
   }
}
-1
gurucharanchouhan171 month ago
Accepted  Solution in java  :)
 
class GFG
{
    static void rotate(int a[][]) 
    {
        int n=a.length;
        
       int temp[][]=new int[n][n];
       
       int k=0;
       for(int j=n-1;j>=0;j--,k++)
       {
           for(int i=0;i<n;i++)
           {
               temp[k][i]=a[i][j];
           }
       }
       for(int i=0;i<n;i++)
       {
           for(int j=0;j<n;j++)
           {
               a[i][j]=temp[i][j];
           }
       }
    }
}
0
bajpayeevinayak2281 month ago
hello coder hope you are doing well this code is one of the simplest method hope you will understand easily you can ask doubt!! 
don't lose hope or don't give up this line is for me hahhaaa!
// { Driver Code Starts//Initial template for C++
#include<bits/stdc++.h>using namespace std;void rotate (vector<vector<int> >& matrix);
// } Driver Code Ends//User function template for C++
/* matrix : given input matrix, you are require to change it in place without using extra space */void rotate(vector<vector<int> >& matrix){   // Your code goes here    int k=1;   int j;   int n=matrix.size();    for(int i=0;i<n;i++)   {       if(i==0)       {           j=0;       }       else{           j=i+1;       }       for(j;j<n;j++)       {           swap(matrix[i][j],matrix[j][i]);// arrange the rows into column which        }   }   for(int col=0;col<n;col++)   {       for(int row=0;row<n/2;row++)       {           swap(matrix[row][col],matrix[n-row-1][col]);//swap every column elements       }   }}
// { 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].resize(n);           for(int j=0; j<n; j++)               cin>>matrix[i][j];        }       rotate(matrix);       for (int i = 0; i < n; ++i)       {           for(int j=0; j<n; j++)               cout<<matrix[i][j]<<" ";           cout<<"\n";       }   }   return 0;}
 // } Driver Code Ends
 
0
jayesh291 month ago
Easy Java Solution
class GFG{
    static void rotate(int mat[][]) {
        tranpose(mat);
        for(int i=0;i<mat.length;i++){
            rev(mat,i);
        }
    }
    static void rev(int[][] mat ,int c){
        int lo = 0,hi=mat.length-1;
        while(lo<hi){
            int temp = mat[lo][c];
            mat[lo][c]=mat[hi][c];
            mat[hi][c]=temp;
            lo++;hi--;
        }
    }
    static void tranpose(int [][] mat){
        for(int i =0;i<mat.length;i++){
            for(int j=i+1;j<mat.length;j++){
                int temp = mat[i][j];
                mat[i][j]=mat[j][i];
                mat[j][i]=temp;
            }
        }
    }
}
0
dsaconquered2 months ago
Algo-
 first transpose the matrix then reverse individual column 
 
 void transpose(vector<vector<int> >& matrix, int n)   {        // code here        for(int i=0;i<n;i++){           for(int j=i+1;j<n;j++){              swap(matrix[i][j],matrix[j][i]);            }       }   }void rotate(vector<vector<int> >& matrix){   // Your code goes here   int n=matrix.size();   transpose(matrix,n);      for(int i=0;i<n;i++){       int low=0,high=n-1;       while(low<high){           swap(matrix[low][i],matrix[high][i]);           low++;high--;       }   }   }
0
visveswar19012612 months ago
#include<stdio.h>void rotate(int a[3][3]){int i,j;printf("The Matrix\n");for(i=0;i<3;i++){ for(j=0;j<3;j++) {  printf("%2d",a[i][j]); } printf("\n");}printf("Transpose of Matrix\n");for(i=0;i<3;i++){ for(j=i;j<3;j++) {  if(i!=j)  {   int t=a[i][j];   a[i][j]=a[j][i];   a[j][i]=t;  } // printf("%d",a[i][j]); } //printf("\n");} for(i=0;i<3;i++){ for(j=0;j<3;j++) {  printf("%2d",a[i][j]); } printf("\n");}printf("Rotation of the Matrix\n");for(i=0;i<3;i++){ for(j=i;j<3;j++) {    if(i==0)    {     int t=a[i][j];  a[i][j]=a[i+2][j];  a[i+2][j]=t;  }  else  {   break;  } }}for(i=0;i<3;i++){ for(j=0;j<3;j++) {  printf("%2d",a[i][j]); } printf("\n");}
}int main(){int a[3][3]= {{1 ,2, 3},             {4 ,5, 6},             {7,8,9}};
   rotate(a);             }
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.