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.

Related Posts

  • Data structures in java
    16 April

    Data Structures in java

    Table of ContentsArray Declare and initialize array in javaAdvantages of arrayDisadvantages of array ExampleArray practice programsStackStack implementation using ArrayStack implementation using LinkedListImplementationPractice ProgramsQueueQueue implementation using arrayQueue implementation using LinkedListImplementationLinkedListImplementationLinkedList Practice ProgramsBinary treeImplementationBinary tree practice programsBinary Search treeImplementationBinary search tree Practice programsTrieImplementationHeapImplementationGraphImplementation Inbuild data structures in javaStringHashMapLinkedHashMapArrayListLinkedListHashSet In this post, we will see about various data […]

  • 29 November

    Top 100+ Java coding interview questions

    Table of ContentsStringQuestion 1 : How to reverse a String in java? Can you write a program without using any java inbuilt methods?Question 2 : Write a java program to check if two Strings are anagram in java?Question 3 : Write a program to check if String has all unique characters in java?Question 4 : […]

  • 18 April

    Minimum Number of Jumps to reach last Index

    Table of ContentsProblemSolution If you want to practice data structure and algorithm programs, you can go through Java coding interview questions. In this post, we will see how to find Minimum Number of Jumps to reach last Index. Problem Given an array A of positive integers possibly zeroes, every index indicating the maximum length of a […]

  • 20 February

    Inorder Successor in a Binary Search Tree

    Table of ContentsProblemSolutionAPPROACH – I :Approach – II:(i) If the target node has a right child :(ii) If the target node does not have a right child :Time Complexity Discussion If you want to practice data structure and algorithm programs, you can go through Java coding interview questions. In this post, we will see how to […]

  • 04 February

    LCA of a K-ary Tree in O(Sqrt(height))

    Table of ContentsProblemSolution If you want to practice data structure and algorithm programs, you can go through Java coding interview questions. In this post, we will see about how to find Lowest Common Ancestor of a K-ary Tree in O(Sqrt(height)).We have already seen how to find LCA of n-ary tree in O(n) complexity. Problem Given a […]

  • 03 February

    Largest Rectangular Area in a Histogram

    Table of ContentsProblemSolutionAPPROACH – IAPPROACH – II (RECURSIVE):Approach – III (ITERATIVE) : If you want to practice data structure and algorithm programs, you can go through Java coding interview questions. In this post, we will see about how to find largest rectangular area in a Histogram. Problem Given an Integer representing number of bars in a […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.