Rotate matrix 90 degrees clockwise and anti-clockwise

Learn how to rotate a square a matrix by 90 degrees in clockwise and anti-clockwise direction.

A matrix is represented by multi-dimensional array.

Clockwise example

Input:
1 2 3 
4 5 6
7 8 9

Output:
7 4 1 
8 5 2
9 6 3

Implementation

This algorithm will work without using any constant space that is it will rotate the matrix in place.

To rotate a matrix we will follow the steps of how we would rotate a square plane.

There is N/2 squares or cycles in a matrix of size N. Process a square one at a time. Run a loop to traverse the matrix a cycle at a time, i.e loop from 0 to N/2 – 1.

Traverse half the size of the matrix and in each loop rotate the element by updating them in clock wise or anti-clockwise direction.

It is similar like rotating a gear lock in one direction, first rotate the first layer, then second layer and so on. If there are inner cycle then repeat the same for them.

Rotate a square matrix in clockwise direction

Rotate a matrix 90 degrees clockwise

As you can see in the above image we rotate each square in the top cycle and then move to the inner cycle.

const rotateMatrixClockWise = (arr, N = arr.length) => {
   for(let x = 0; x < N / 2; x++){
     for(let y = x; y < N - x - 1; y++){

       //Store the left value and start the rotation from here
       let temp = arr[x][y];

       // Move values from left to top 
       arr[x][y] = arr[N - 1 - y][x];

       // Move values from top to right 
       arr[N - 1 - y][x] = arr[N - 1 - x][N - 1 - y];

       // Move values from right to bottom 
       arr[N - 1 - x][N - 1 - y] = arr[y][N - 1 - x];

       // Move values from bottom to left 
       arr[y][N - 1 - x] = temp;
     }
   }
  
  return arr;
}
Input: 
const arr = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]];
console.log(rotateMatrixClockWise(arr));

Output:
[[13, 9, 5, 1], [14, 10, 6, 2], [15, 11, 7, 3], [16, 12, 8, 4]]

Time complexity: O(N ^ 2).
Space complexity: O(1).


Rotate a square matrix by 90 degrees in anti-clockwise direction

Rotate a matrix 90 degrees in anti-clockwise direction

Same as we did for clockwise direction for anti-clock wise direction will rotate in opposite direction.

const rotateMatrixAntiClockWise = (arr, N = arr.length) => {
   for(let x = 0; x < N / 2; x++){
     for(let y = x; y < N - x - 1; y++){

       //Store the right value and start the rotation from here
       let temp = arr[x][y];
       
       // Move values from right to top 
       arr[x][y] = arr[y][N - 1 - x];

       // Move values from bottom to right 
       arr[y][N - 1 -x] = arr[N - 1 - x][N - 1 - y];
 
       // Move values from left to bottom 
       arr[N - 1 - x][N - 1 - y] = arr[N - 1 -y][x];

       // Assign temp to left 
       arr[N - 1 - y][x] = temp;
     }
   }
  
  return arr;
}
Input: 
const arr = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]];
console.log(rotateMatrixClockWise(arr));

Output:
[[4, 8, 12, 16], [3, 7, 11, 15], [2, 6, 10, 14], [1, 5, 9, 13]]

Time complexity: O(N ^ 2).
Space complexity: O(1).