Rotate matrix elements clockwise | Practice | GeeksforGeeks

Given two integers M, N, and a 2D matrix Mat of dimensions MxN, clockwise rotate the elements in it.
Example 1:
Input:
M=3,N=3
Mat=[[1,2,3],[4,5,6],[7,8,9]]
Output:
4 1 2
7 5 3
8 9 6
Explanation:
Rotating the matrix clockwise gives this result.
Example 2:
Input:
M=2,N=3
Mat=[[1,2,3],[2,3,3]]
Output:
2 1 2
3 3 3
Explanation:
Rotating the matrix clockwise gives this result.

Your Task:
You don't need to read input or print anything. Your task is to complete the function rotateMatrix() which takes two integers M, N, and a 2D matrix as input parameters and returns the clockwise rotated matrix.

Expected Time Complexity:O(M*N)
Expected Auxillary Space:O(1)

Constraints:
1<=M,N,Mat[i][j]<=1000
0
sandeepkmrnayak1 month ago
Simple C++ Solution :
int l = 0;
       int r = N-1;
       int t = 0;
       int b = M-1;
       while(l<r && t<b)
       {
           int x = Mat[t+1][l];
           for(int i = l;i<=r;i++)
           {
               swap(x,Mat[t][i]);
           }
           t++;
           for(int i = t;i<=b;i++)
           {
               swap(x,Mat[i][r]);
           }
           r--;
           for(int i = r;i>=l;i--)
           {
               swap(x,Mat[b][i]);
           }
           b--;
           for(int i = b;i>=t;i--)
           {
               swap(x,Mat[i][l]);
           }
           l++;
       }
       return Mat;
0
omdubey99773 months ago
class Solution {   int[][] rotateMatrix(int M, int N, int Mat[][]) {       // code here       int row = 0, col = 0;       int prev, curr;              while (row < M && col < N)       {               if (row + 1 == M || col + 1 == N)               break;                          prev = Mat[row + 1][col];                          for (int i = col; i < N; i++)           {               curr = Mat[row][i];               Mat[row][i] = prev;               prev = curr;           }           row++;                          for (int i = row; i < M; i++)           {               curr = Mat[i][N-1];               Mat[i][N-1] = prev;               prev = curr;           }           N--;                          if (row < M)           {               for (int i = N-1; i >= col; i--)               {                   curr = Mat[M-1][i];                   Mat[M-1][i] = prev;                   prev = curr;               }           }           M--;                          if (col < N)           {               for (int i = M-1; i >= row; i--)               {                   curr = Mat[i][col];                   Mat[i][col] = prev;                   prev = curr;               }           }           col++;       }       return Mat;    }   }
0
sharmatanish6564 months ago
class Solution { public:   vector<vector<int>> rotateMatrix(int n, int m, vector<vector<int>> Mat) {       vector<vector<int>> rotatedMatrix=Mat;       int k=0,l=0;       int curr,prev;                     while(k<n && l<m)       {           if(k+1==n || l+1==m)               break;           prev=Mat[k+1][l];           for(int i=l;i<m;i++)           {               curr=Mat[k][i];               Mat[k][i]=prev;               prev=curr;           }           k++;           for(int i=k;i<n;i++)           {               curr=Mat[i][m-1];               Mat[i][m-1]=prev;               prev=curr;           }           m--;           if(k < n)           {               for(int i=m-1;i>=l;i--)               {                   curr=Mat[n-1][i];                   Mat[n-1][i]=prev;                   prev=curr;               }           }           n--;           if(l < m)           {               for(int i=n-1;i>=k;i--)               {                   curr=Mat[i][l];                   Mat[i][l]=prev;                   prev=curr;               }                          }           l++;       }       rotatedMatrix=Mat;       return rotatedMatrix;   }};
0
anutiger7 months ago
    void helper(vector<vector<int>> &mat, int i , int j,int r ,int c){
        int r1 = i;
        while(r1 < r){
            swap(mat[r1][j],mat[r1 + 1][j]);
            r1++;
        }
        int c1 = j;
        while(c1 < c){
            swap(mat[r][c1],mat[r][c1 + 1]);
            c1++;
        }
        r1 = r;
        while(r1 > i){
            swap(mat[r1][c],mat[r1 - 1][c]);
            r1--;
        }
        c1 = c;
        while(c1 - 1 > j){
            swap(mat[i][c1],mat[i][c1 - 1]);
            c1--;
        }
    }
    vector<vector<int>> rotateMatrix(int M, int N, vector<vector<int>> mat) {
        M--;N--;
        int i = 0;
        int j = 0;
        while(i < M  and j < N){
            helper(mat,i,j,M,N);
            i++;j++;M--;N--;
        }
        return mat;
    }
-1
Suraj kumar dubey11 months ago
Suraj kumar dubey
int row=0,col=0;        int prev,curr;        while(row<m&&col<n) {="" if(row+1="=M||col+1==N)" break;="" prev="mat[row+1][col];" store="" the="" first="" value="" which="" we="" have="" to="" move="" for(int="" i="col;i&lt;N;i++)" clockwise="" rotation="" of="" fist="" row="" {="" curr="mat[row][i];" mat[row][i]="prev;" prev="curr;" }="" row++;="" for(int="" i="row;i&lt;M;i++)" clockwise="" rotation="" of="" last="" column="" {="" curr="mat[i][N-1];" mat[i][n-1]="prev;" prev="curr;" }="" n--;="" if(row<m)="" clockwise="" rotation="" of="" last="" row="" {="" for(int="" i="N-1;i">=col;i--)            {                curr=mat[M-1][i];                mat[M-1][i]=prev;                prev=curr;            }            }            M--;
            if(col<n) clockwise="" rotation="" of="" fist="" column="" {="" for(int="" i="M-1;i">=row;i--)                {                    curr=mat[i][col];                    mat[i][col]=prev;                    prev=curr;                }            }
            col++;        }        return mat;    }
0
shivam__9911 months ago
shivam__99
class Solution {  public:    vector<vector<int>> rotateMatrix(int n, int m, vector<vector<int>> mat) {        vector<vector<int>> result= mat;        int row = 0, col = 0;         int prev, curr;         int lastRow= n;        int lastCol= m;        while (row < lastRow && col < lastCol)          {              if (row + 1 == lastRow || col + 1 == lastCol)  break;                prev = mat[row + 1][col];             //for the first row which is in bounds            for (int i = col; i < lastCol; i++)             {                 curr = mat[row][i];                 mat[row][i] = prev;                 prev = curr;             }             row++;             //for the last column which is in bounds            for (int i = row; i < lastRow; i++)             {                 curr = mat[i][lastCol-1];                 mat[i][lastCol-1] = prev;                 prev = curr;             }             lastCol--;               //for the last row which is in bounds            if (row < lastRow)             {                 for (int i = lastCol-1; i >= col; i--)                 {                     curr = mat[lastRow-1][i];                     mat[lastRow-1][i] = prev;                     prev = curr;                 }             }             lastRow--;             //for the first row which is in bounds            if (col < lastCol)             {                 for (int i = lastRow-1; i >= row; i--)                 {                     curr = mat[i][col];                     mat[i][col] = prev;                     prev = curr;                 }             }             col++;         }         result = mat;        return result;    }};
0
abhikarsh gupta1 year ago
abhikarsh gupta
Given as Hard Tag in tutorial and Easy tag here.. we should get more points for this question.
0
anmol mittal2 years ago
anmol mittal
changing the existing matrix itself, O(1) solution using swap functionhttps://ide.geeksforgeeks.o...
0
Naveen Saladi3 years ago
Naveen Saladi
// easy code
void rotate(int a[100][100],int m,int n){   int x=0,i,j,temp1,temp2,n1=n,m1=m;   if(n!=1 && m!=1)   {    while(n>0 && m>0)    {        i=0+x;        for(j=n-1;j>=0+x;j--)        {            if(i==0+x && j==n-1)                temp1=a[i][j];            else                a[i][j+1]=a[i][j];        }
        j=n-1;        for(i=m-1;i>=0+x;i--)        {            if(j==n-1 && i==m-1)                temp2=a[i][j];            else if(j==n-1 && i==0+x)                a[i+1][j]=temp1;            else                a[i+1][j]=a[i][j];        }
        i=m-1;        for(j=0+x;j<=n-1;j++)        {            if(i==m-1 && j==n-1)                a[i][j-1]=temp2;            else if(i==m-1 && j==0+x)                temp1=a[i][j];            else                a[i][j-1]=a[i][j];        }
        j=0+x;        for(i=0+x;i<=m-1;i++)        {            if(i==0+x && j==0+x)                temp2=a[i][j];            else if(i==m-1 && j==0+x)                a[i-1][j]=temp1;            else                a[i-1][j]=a[i][j];        }
        m=m-1;        n=n-1;        x=x+1;
        if(x==m || x==n || x==m-1 || x==n-1)            break;
    }   }        for(i=0;i<m1;i++) for(j="0;j&lt;n1;j++)" cout<<a[i][j]<<"="" ";="" }="">
0
vikram4 years ago
vikram
13 691 58 75 26 13 4 96 50 52 84 84 25 54 62 40 47 9 71 31 38 77 7 42 99 1 40 97 72 91 31 64 81 89 38 7 53 41 2 3 92 37 38 69 90 100 8 89 60 30 19 49 6 25 91 56 77 82 4 48 72 35 11 53 23 100 59 27 41 12 81 84 100 19 52 90 18 11 78
the correct output shown for the test case is wrong. Please checkIt should be 96 91 58 75 26 13 4 25.....  instead of 96 91 58 75 26 13 54...
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.