Rotate matrix by 90 degree

Rotate matrix by 90 degree OR
Turn an 2D array by 90 degree OR

Rotate a two dimensional array OR
Given N*M matrix, rotate it by 90 degree to left and right.

Given N*M Matrix, Rotate it by 90 degrees.
 
Lets understand the problem statement graphically and it will be more clear,  




Algorithm


 There are 2 ways to Rotate a Matrix by 90 degrees
  1. In Place.
  2. Using extra Memory.

We already saw how to Rotate a Matrix In-Place: Rotate Matrix to 90 degrees Inplace.

In this post, we will focus on
Rotating a Matrix by 90 degrees clockwise using Extra memory.

For Rotating a matrix to 90 degrees clockwise, We need to transform each row of a Matrix to a column in rotated matrix. (Check the rotated matrix image above)

It is very easy to solve this problem if it is well understood.  

What will be Rows and Columns of Rotated Result Matrix?
For rotating matrix to 90 degrees, we need to transform rows into columns and columns to rows in a result matrix. So number of rows in rotated matrix will be equal to number of columns of original matrix and number of columns in rotated matrix will be equal to number of rows.
int[][] rotatedMatrix = new int[colsOfOriginalMatrix][rowsOfOriginalMatrix]

So to summarize our Algorithm:
  1. First row of original Matrix will be last column of Rotated Matrix.
  2. Second row of original Matrix will be second last column of Rotated Matrix.
  3. Third row of original Matrix will be third last column of Rotated Matrix.
    and so on.


Java Program to Rotate a Matrix by 90 degrees


public class RotateMatrix {

 public static void main(String[] args) {
  new RotateMatrix();
 }

 public RotateMatrix() {
  int[][] matrix = {
    {1,  2,  3,  4},
    {5,  6,  7,  8},
    {9,  10, 11, 12},
    {13, 14, 15, 16},
    {17, 18, 19, 20}
  };

  System.out.println("Original Matrix :");
  printMatrix(matrix);

  int[][] rotatedMatrix = rotateMatrixBy90DegreeCounterClockwise(matrix);
  System.out.println("Rotated Matrix :");
  printMatrix(rotatedMatrix);
 }

 //Rotate Matrix to 90 degree toward Right(clockwise)
 private int[][] rotateMatrixBy90DegreeClockwise(int[][] matrix) {

  int totalRowsOfRotatedMatrix = matrix[0].length; //Total columns of Original Matrix
  int totalColsOfRotatedMatrix = matrix.length; //Total rows of Original Matrix

  int[][] rotatedMatrix = new int[totalRowsOfRotatedMatrix][totalColsOfRotatedMatrix];

  for (int i = 0; i < matrix.length; i++) {
   for (int j = 0; j < matrix[0].length; j++) {
    rotatedMatrix[j][ (totalColsOfRotatedMatrix-1)- i] = matrix[i][j]; 
   }
  }
  return rotatedMatrix;
 }

 //Rotate Matrix to 90 degree toward Left(counter clockwise)
 private int[][] rotateMatrixBy90DegreeCounterClockwise(int[][] matrix) {

  int totalRowsOfRotatedMatrix = matrix[0].length; //Total columns of Original Matrix
  int totalColsOfRotatedMatrix = matrix.length; //Total rows of Original Matrix

  int[][] rotatedMatrix = new int[totalRowsOfRotatedMatrix][totalColsOfRotatedMatrix];

  for (int i = 0; i < matrix.length; i++) {
   for (int j = 0; j < matrix[0].length; j++) {
    rotatedMatrix[(totalRowsOfRotatedMatrix-1)-j][i] = matrix[i][j]; 
   }
  }
  return rotatedMatrix;
 }

 private static void printMatrix(int[][] matrix){
  for (int i = 0; i < matrix.length; i++) {
   for (int j = 0; j < matrix[0].length; j++) {
    System.out.print(matrix[i][j] + "   "); 
   }
   System.out.println();
  }
 }
}



You may also like to see


Rotate Matrix by 90 degrees clockwise Inplace


Print Matrix in Spiral order (Iterative way)

Transpose of M*N Matrix in Java

Count zeros in a row wise and column wise sorted matrix

Find middle element of a linked list

Union and Intersection of Two Sorted Arrays

Merge two sorted arrays in Java

How is ambiguous overloaded method call resolved in java


Enjoy !!!! 

If you find any issue in post or face any error while implementing, Please comment.

Post a Comment