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 anti-clockwise direction without using any extra space. 

Example 1:
Input:
N = 3 
matrix[][] = {{1, 2, 3},
              {4, 5, 6}
              {7, 8, 9}}
Output: 
Rotated Matrix:
3 6 9
2 5 8
1 4 7

Example 2:
Input:
N = 2
matrix[][] = {{1, 2},
              {3, 4}}
Output: 
Rotated Matrix:
2 4
1 3


Your Task:
You dont need to read input or print anything. Complete the function rotateby90() which takes the matrix as input parameter and rotates it by 90 degrees in anti-clockwise direction without using any extra space. You have to modify the input matrix in-place. 

Expected Time Complexity: O(N2)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N ≤ 100
1 <= matrix[][] <= 1000
-1
mannukhurana103974 days ago
java 
 
static void rotateby90(int arr[][], int n) 
    { 
        for(int i=0; i<n-1; i++){
            for(int j=i+1; j<n; j++){
                int temp = arr[i][j];
                arr[i][j] = arr[j][i];
                arr[j][i] = temp;
            }
        }
        
        for(int i=0; i<n; i++){
            int z = n-1;
            for(int j=0; j<n/2; j++){
                int temp = arr[j][i] ;
                arr[j][i] = arr[z-j][i];
                 arr[z-j][i] = temp;
            }
        }
    }
0
20bd5a05201 week ago
static void rotateby90(int arr[][], int n)    {        // code here       for(int i=0;i<arr.length;i++)       {           for(int j=i;j<arr[i].length;j++)           {               int temp=arr[i][j];               arr[i][j]=arr[j][i];               arr[j][i]=temp;           }       }                 for(int i=0;i<arr[0].length;i++)       {           int x=0;           int j=0;           while(j<arr.length/2)           {               int swap=arr[x][i];               arr[x][i]=arr[arr.length-1-j][i];               arr[arr.length-1-j][i]=swap;               j++;               x++;           }       }                 }
+1
vikasnayakmmmut1 week ago
do in simple way:-
 void rotateby90(vector<vector<int> >& matrix, int n)    {        for(int i=0;i<n;i++){           for(int j=i+1;j<n;j++)           {               int t=matrix[i][j];               matrix[i][j]=matrix[j][i];               matrix[j][i]=t;           }       }       reverse(matrix.begin(),matrix.end());   } 
+4
sharma9897akash1 week ago
FIRST DO TRANSPOSESWAP UPPPERMOST ROW  WITH LOWERMOST ROW CONTINUE UNTILL ALL ROWS ARE NOT SWAPPED
FIRST DO TRANSPOSE
SWAP UPPPERMOST ROW  WITH LOWERMOST ROW CONTINUE UNTILL ALL ROWS ARE NOT SWAPPED
0
devashishsharma2602 weeks ago
JAVA SOLUTION
static void rotateby90(int matrix[][], int n){    // code here    for(int i=0;i<n;i++){        for(int j=i+1;j<n;j++){           int temp=matrix[i][j];           matrix[i][j]=matrix[j][i];           matrix[j][i]=temp;        }    }    for (int i = 0; i < n; i++) {        int start = 0;        int end = n - 1;        while (start < end)        {            matrix = swap(matrix,start,i,end,i);            start++;            end--;        }    }} static int[][] swap(int[][] arr, int start,                            int i, int end, int j){    int temp = arr[start][i];    arr[start][i] = arr[end][j];    arr[end][j] = temp;    return arr;}
0
rg_19992 weeks ago
Simple C++ solution:time complexity - O(n*n)space complexity - O(1) (in place) 
class Solution
{   
   public:
   void transpose(vector<vector<int>>&mat, int n)
   {
       for(int i=0;i<n;i++)
       {
           for(int j=i;j<n;j++)
           {
               swap(mat[i][j], mat[j][i]);
           }
       }
   }
   void reverse_col(vector<vector<int>>&mat, int n)
   {
       for(int i=0;i<n;i++)
       {
           for(int j=i;j<n;j++)
           {
               swap(mat[i], mat[j]);
           }
       }
   }
   void rotateby90(vector<vector<int> >& matrix, int n) 
   { 
       // code here 
       transpose(matrix, n);
       reverse_col(matrix, n);
   } 
};
+1
aloksinghbais023 weeks ago
C++ solution having time complexity as O(n*n) and space complexity as O(n*n) is as follows :- 
 
Execution Time :- 0.09 / 1.1 sec
 
void rotateby90(vector<vector<int> >& matrix, int n)    {       vector<vector<int>> temp(n,vector<int>(n));       for(int i1 = 0,j = n - 1; j >= 0; i1++,j--){           for(int j1 = 0,i = 0; i < n; j1++,i++){               temp[i1][j1] = matrix[i][j];           }       }              for(int i = 0; i < n; i++){           for(int j = 0; j < n; j++){               matrix[i][j] = temp[i][j];           }       }   } 
0
rathirao053 weeks ago
static void rotateby90(int[,] matrix) {  for(int j = 2; j>= 0; j--)  {   for (int i = 0; i <= 2; i++)   {    Console.Write(matrix[i,j]);    Console.Write('\t');   }   Console.WriteLine();  } }
+1
amrit4604 weeks ago
void rotateby90(vector<vector<int> >& matrix, int n) 
    { 
        // code here 
        for (int i=0; i<n-1; i++)
        {
            for (int j=i+1; j<n; j++)
            {
                swap(matrix[i][j],matrix[j][i]);
            }
        }
        reverse(matrix.begin(),matrix.end());
    }
0
sunboykenneth1 month ago
class Solution
{   
    private:
    void swap(int& t1, int& t2) {
        int tmp = t1;
        t1 = t2;
        t2 = tmp;
    }
    
    public:
    //Function to rotate matrix anticlockwise by 90 degrees.
    void rotateby90(vector<vector<int> >& matrix, int n) 
    { 
        if (n < 2) {
            return;
        }
        
        int midIndex = (n / 2) - 1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= midIndex; j++) {
                swap(matrix[i][j], matrix[i][n - 1 - j]);
            }
        }
        
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; 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.