Rotate Matrix by 90 degrees in java

Rotate matrix by 90 degrees in java

In this article, we will look into another interesting problem related to 2D Arrays. Given a Matrix of N X N Dimension we have to Rotate matrix by 90 degrees. We will perform Rotation both Clockwise i.e. Right Rotation and Anti-Clockwise i.e. Left Rotation respectively. We will discuss each operation in detail along with a hint to perform Rotation of Matrix K times.

Note: To Perform Any type of Rotation, The Matrix should have equal dimensions (N X N) i.e. No. of Rows in Matrix = No. of Columns in Matrix.

Clockwise or Right Rotate a Matrix

In this type, we need to Right Rotate the given N X N Matrix by 90 degrees. Let us understand this with an example:

Basically, we need to start from the last row in the Original Matrix and need to make each row as a column to rotate the matrix in Clockwise direction.

So, let us look at the approach to do this :

  • We will first transpose of given matrix or 2D Array. By Transpose, we mean each Row of the matrix becomes a Column and vice-versa. In simple words, each Element in ith row is replaced by corresponding element in the ith column. We will do this in code by Swapping the elements at the ith row and column respectively. So, considering the above matrix the Transpose will be:
  • After doing transpose, we need to just reverse the contents/elements of each row. Since each Row of the matrix is a 1D Array itself, we just reverse each array or row after that we would have the Right Rotated Matrix. So after reversing the Transpose Matrix we get the result.

Implementation in Java

Now, let us look at the code to cover this approach considering the same example as discussed above.

Output:

Time Complexity: The time complexity of performing rotation is O(n2) as we traverse all the elements of the matrix while interchanging them.

Anti-Clockwise or Left Rotate a Matrix

In this type, we need to Left Rotate the given N X N Matrix by 90 degrees. Let us understand this with an example:

Basically, we need to start from the last row in the Original Matrix and need to make each column as a row to rotate the matrix in anti clockwise direction.

So, let us look at the approach to do this :

  • Like the approach we followed above in Right Rotate, we will first transpose the given Matrix or 2D Array. We convert elements of each Row of the matrix becomes a Column and vice-versa. So, considering the above matrix the Transpose will be:
  • After getting the transpose, Instead of reversing the Rows like we did in Right Rotate, Here we reverse contents of each Column in the Matrix. After doing this, we will have our resultant Left Rotated Matrix:

Now let us look at the implementation in code:

Output:

Time Complexity: The time complexity for this type is also O(n2).

How to Rotate Matrix K Times?

Now if we want to Rotate a matrix K times, Depending on the type of rotation just call the rightRotate() or leftRotate() method inside a loop K times and print the matrix once like this:

So that’s all about how to Rotate Matrix by 90 degrees in java. You can try out the code with different examples and let us know your queries.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *