Rotate a 2D array without using extra space | Practice | GeeksforGeeks

Given a N x N 2D matrix Arr representing an image. Rotate the image by 90 degrees (anti-clockwise). You need to do this in place. Note that if you end up using an additional array, you will only receive partial score.
Example 1:
Input:
N = 3
Arr[][] = {{1,  2,  3}
           {4,  5,  6}
           {7,  8,  9}}
Output:
 3  6  9 
 2  5  8 
 1  4  7 
Explanation: The given matrix is rotated
by 90 degree in anti-clockwise direction.
Example 2:
Input:
N = 4
Arr[][] = {{1,  2,  3,  4}
           {5,  6,  7,  8}
           {9, 10, 11, 12}
           {13, 14, 15, 16}}
Output:
 4  8 12 16 
 3  7 11 15 
 2  6 10 14 
 1  5  9 13
Explanation: The given matrix is rotated
by 90 degree in anti-clockwise direction.

Your Task:  
You don't need to read input or print anything. Your task is to complete the function rotate() which takes the 2D array of integers arr and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(N*N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 1000
1 ≤ Arr[i][j] ≤ 1000
0
siwaniagrawal2 days ago

void rotateMatrix(vector<vector<int>>& arr, int N) {
//swap along diagonal
     for(int i = 0; i < N; i++)
   {
       for(int j = 0; j <= i; j++)
       {
          swap(arr[i][j],arr[j][i]);
       }
    }
 // swap rows
   for(int i=0;i<N/2;i++){
       swap(arr[i],arr[N-i-1]);
   }
}
 
+1
velspace012 weeks ago
Java Sol
 void rotateMatrix(int Mat[][], int n) {
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                int temp=Mat[i][j];
                Mat[i][j]=Mat[j][i];
                Mat[j][i]=temp;
            }
        }
        int r=n-1;
        for(int i=0;i<n/2;i++){
            for(int j=0;j<n;j++){
                int temp=Mat[i][j];
                Mat[i][j]=Mat[r][j];
                Mat[r][j]=temp;
            }
            r--;
        }
    }
0
roy99mustang3 weeks ago
Easy python solution:
class Solution:
    
    def rotateMatrix(self,arr, n):
        # for better clarity
        def swap(a, b, c, d):
            arr[a][b], arr[c][d] = arr[c][d], arr[a][b]

        # layers till the center
        for l in range(n//2):
            r = n-1-l
            # x iterates over elements
            for x in range(l+1, n-l):
                # top -> right
                swap(x, r, l, x)
                # right -> bottom
                swap(r, n-1-x, x, r)
                # bottom -> left
                swap(n-1-x, l, r, n-1-x)
+1
shourya26rathore3 months ago
	void rotateMatrix(vector<vector<int>>& mat, int n) {
	   int s=0,e=n-1,i,j;
	  for(i = 0; i <n; i++){
         for( j = 0; j <i; j++){
              swap(mat[i][j],mat[j][i]);
            }
        }
            for( i = 0; i < n; i++){
  while(s<=e)
          {
              swap(mat[s][i],mat[e][i]);
              s++;
              e--;
          }
          
        }
	}

0
aniketag7873 months ago
class Solution {   void rotateMatrix(int arr[][], int n) {       // code here              for(int i = 0; i < arr.length; i++){           for(int j = i + 1; j<arr[0].length; j++){               int temp = arr[i][j];               arr[i][j] = arr[j][i];               arr[j][i] = temp;           }       }              for(int i = 0; i < arr.length; i++){           int left  = 0;           int right = arr[i].length - 1;                      while(left < right){               int temp = arr[left][i];               arr[left][i] = arr[right][i];               arr[right][i] = temp;               left++;               right--;           }       }   }}
0
forcer3 months ago
 
//SIMPLE APPROACH C++:-
for(int i=0;i<n;i++)    {        reverse(arr[i].begin(),arr[i].end());    }    for(int i=0;i<n;i++)    {        for(int j=0;j<i;j++)swap(arr[i][j],arr[j][i]);    }
+1
vivekpatel72023 months ago
void rotateMatrix(vector<vector<int>>& arr, int n) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= i ; j++) {
      swap(arr[i][j] , arr[j][i]);
    }
  }
  // If it is clock wise then just reverse
  // for (int i = 0; i < n ; i++) {
  //   reverse(arr[i].begin(), arr[i].end());
  // }
  
  // For anti clockwise
  for (int i = 0; i < n; ++i)
  {
    /* code */
    for (int j = 0; j < n / 2; ++j)
    {
      swap(arr[j][i], arr[n - j - 1][i]);
    }
  }


}
+1
keshrishivam41144 months ago
def rotateMatrix(self,arr, n):       for i in range(n):           for j in range(1+i,n):               arr[i][j],arr[j][i]=arr[j][i],arr[i][j]       for k in range(n//2):           for l in range(n):               arr[k][l],arr[n-1-k][l]=arr[n-1-k][l],arr[k][l]       return arr
0
shiva10904 months ago
 for(int i = 0; i < n; i++)    {        for(int j = 0; j < n; j++)        {            if(i > j)               swap(arr[i][j] , arr[j][i]);        }    }        for(int i = 0; i < n/2; i++)    {        for(int j = 0; j < n; j++)        {            swap(arr[i][j] , arr[n-i-1][j]);        }    }    
0
shiva10904 months ago
  for(int i = 0; i < n; i++)    {        for(int j = 0; j < i; j++)        {               swap(arr[i][j] , arr[j][i]);        }    }        for(int i = 0; i < n/2; i++)    {        for(int j = 0; j < n; j++)        {            swap(arr[i][j] , arr[n-i-1][j]);        }    }    
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.