Given an n à m matrix mat[][], rotate all its elements by one position in clockwise, layer by layer (each rectangular layer (ring)) of the matrix should be rotated independently, return the modified matrix.
[Expected Approach ] - Using Layer Wise Rotation - O( n à m) Time and O(1) Space
The idea is to process the matrix one layer (ring) at a time. For each layer, we move all its boundary elements one step clockwise. Since moving one element can overwrite another, we first save its value in a temporary variable and keep updating it as we traverse the boundary. We repeat this for every layer from the outside to the inside.
Initialize the matrix boundaries (top, bottom, left, and right).
Process one layer of the matrix at a time from the outermost layer.
Store the first element of the next row in a temporary variable.
Move the boundary elements in the order: top row â right column â bottom row â left column.
Shrink the boundaries to move to the next inner layer.
Repeat until all layers are rotated and return the modified matrix.
C++
#include<iostream>#include<vector>usingnamespacestd;vector<vector<int>>rotateMatrix(vector<vector<int>>&mat){intm=mat.size();intn=mat[0].size();introw=0,col=0;intprev,curr;// Rotate each layer independentlywhile(row<m&&col<n){// If there is only one row or one column leftif(row+1==m||col+1==n)break;// Store the first element of the next rowprev=mat[row+1][col];// Move elements of the top rowfor(inti=col;i<n;i++){curr=mat[row][i];mat[row][i]=prev;prev=curr;}row++;// Move elements of the right columnfor(inti=row;i<m;i++){curr=mat[i][n-1];mat[i][n-1]=prev;prev=curr;}n--;// Move elements of the bottom rowif(row<m){for(inti=n-1;i>=col;i--){curr=mat[m-1][i];mat[m-1][i]=prev;prev=curr;}}m--;// Move elements of the left columnif(col<n){for(inti=m-1;i>=row;i--){curr=mat[i][col];mat[i][col]=prev;prev=curr;}}col++;}returnmat;}intmain(){vector<vector<int>>mat={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};vector<vector<int>>ans=rotateMatrix(mat);// Print the rotated matrixfor(auto&row:ans){for(intval:row)cout<<val<<" ";cout<<endl;}return0;}
Java
importjava.util.*;publicclassGFG{staticint[][]rotateMatrix(int[][]mat){intm=mat.length;intn=mat[0].length;introw=0,col=0;intprev,curr;// Rotate each layer independentlywhile(row<m&&col<n){// If there is only one row or one column leftif(row+1==m||col+1==n)break;// Store the first element of the next rowprev=mat[row+1][col];// Move elements of the top rowfor(inti=col;i<n;i++){curr=mat[row][i];mat[row][i]=prev;prev=curr;}row++;// Move elements of the right columnfor(inti=row;i<m;i++){curr=mat[i][n-1];mat[i][n-1]=prev;prev=curr;}n--;// Move elements of the bottom rowif(row<m){for(inti=n-1;i>=col;i--){curr=mat[m-1][i];mat[m-1][i]=prev;prev=curr;}}m--;// Move elements of the left columnif(col<n){for(inti=m-1;i>=row;i--){curr=mat[i][col];mat[i][col]=prev;prev=curr;}}col++;}returnmat;}publicstaticvoidmain(String[]args){int[][]mat={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};int[][]ans=rotateMatrix(mat);// Print the rotated matrixfor(int[]row:ans){for(intval:row)System.out.print(val+" ");System.out.println();}}}
Python
defrotateMatrix(mat):m=len(mat)n=len(mat[0])row=0col=0# Rotate each layer independentlywhilerow<mandcol<n:# If there is only one row or one column leftifrow+1==morcol+1==n:break# Store the first element of the next rowprev=mat[row+1][col]# Move elements of the top rowforiinrange(col,n):curr=mat[row][i]mat[row][i]=prevprev=currrow+=1# Move elements of the right columnforiinrange(row,m):curr=mat[i][n-1]mat[i][n-1]=prevprev=currn-=1# Move elements of the bottom rowifrow<m:foriinrange(n-1,col-1,-1):curr=mat[m-1][i]mat[m-1][i]=prevprev=currm-=1# Move elements of the left columnifcol<n:foriinrange(m-1,row-1,-1):curr=mat[i][col]mat[i][col]=prevprev=currcol+=1returnmat# Driver Codeif__name__=="__main__":mat=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]ans=rotateMatrix(mat)# Print the rotated matrixforrowinans:forvalinrow:printval,print
C#
usingSystem;classGFG{staticint[][]rotateMatrix(int[][]mat){intm=mat.Length;intn=mat[0].Length;introw=0,col=0;intprev,curr;// Rotate each layer independentlywhile(row<m&&col<n){// If there is only one row or one column leftif(row+1==m||col+1==n)break;// Store the first element of the next rowprev=mat[row+1][col];// Move elements of the top rowfor(inti=col;i<n;i++){curr=mat[row][i];mat[row][i]=prev;prev=curr;}row++;// Move elements of the right columnfor(inti=row;i<m;i++){curr=mat[i][n-1];mat[i][n-1]=prev;prev=curr;}n--;// Move elements of the bottom rowif(row<m){for(inti=n-1;i>=col;i--){curr=mat[m-1][i];mat[m-1][i]=prev;prev=curr;}}m--;// Move elements of the left columnif(col<n){for(inti=m-1;i>=row;i--){curr=mat[i][col];mat[i][col]=prev;prev=curr;}}col++;}returnmat;}staticvoidMain(){int[][]mat={newint[]{1,2,3,4},newint[]{5,6,7,8},newint[]{9,10,11,12},newint[]{13,14,15,16}};int[][]ans=rotateMatrix(mat);// Print the rotated matrixforeach(varrowinans){foreach(varvalinrow)Console.Write(val+" ");Console.WriteLine();}}}
JavaScript
functionrotateMatrix(mat){letm=mat.length;letn=mat[0].length;letrow=0,col=0;letprev,curr;// Rotate each layer independentlywhile(row<m&&col<n){// If there is only one row or one column leftif(row+1===m||col+1===n)break;// Store the first element of the next rowprev=mat[row+1][col];// Move elements of the top rowfor(leti=col;i<n;i++){curr=mat[row][i];mat[row][i]=prev;prev=curr;}row++;// Move elements of the right columnfor(leti=row;i<m;i++){curr=mat[i][n-1];mat[i][n-1]=prev;prev=curr;}n--;// Move elements of the bottom rowif(row<m){for(leti=n-1;i>=col;i--){curr=mat[m-1][i];mat[m-1][i]=prev;prev=curr;}}m--;// Move elements of the left columnif(col<n){for(leti=m-1;i>=row;i--){curr=mat[i][col];mat[i][col]=prev;prev=curr;}}col++;}returnmat;}// Driver Codeletmat=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]];letans=rotateMatrix(mat);// Print the rotated matrixfor(constrowofans){console.log(row.join(" "));}