
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program for Rotate the Matrix Right by K times
Array is a linear data structure that is used to store a group of elements with similar data types. It stores data in a sequential manner. When we create an array of two dimensions, i.e., rows and columns, we call it a matrix.
In this article, we will create a matrix of M x N and try to rotate it right by K times. Here, M and N are the size of the row and column, respectively. K is the number of times we want to rotate the matrix.
Example Scenarios
Let's understand what matrix rotation is with the following visual representations
When we rotate the matrix 1 time:

When we rotate the matrix 2 times:

We can see in the above diagrams that when we are rotating the matrix to right 1 time then, the first column gets shifted to 2nd position and when we are rotating the matrix to right 2 times then, the first column gets shifted to 3rd position.
How to Rotate Matrix to Right?
Following are the steps to rotate a given matrix in Java -
-
Step 1: Take a while loop that will run till the number of times the rotation has to be done. When we rotate the array greater than the size of column, the rotation does not affect the original array.
-
Step 2: Inside the while loop, we will take two for loops, the first loop will run till the size of the row, and during each iteration, it will store the value of the last column of the matrix to A temporary variable. The second for loop will take the remaining column value and shift it to the right.
-
Step 3: After execution of the second for loop, we will store the value of the temporary variable in the 1st column of the matrix.
-
Step 4: When both of the for loops finish their execution then we will decrement the number of rotations by 1. After that, we will check whether the rotation is greater than 0 or not. If it is greater than 0 then again the for loops will repeat their execution and work as discussed in steps 2 and 3.
Example: Right Rotation of Matrix by K times
In the following Java program, we will see the implementation of the above approach.
public class Mat { public static void main(String[] args) { int row=4; int col=4; int arr[][] = {{7, 2, 1, 3}, {6, 1, 3, 7}, {4, 9, 8, 0}, {8, 0, 1, 2}}; // Number of rotation int k = 3 % col; int temp; System.out.println("The given matrix: "); // loop to print matrix value for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } // loop to perform matrix rotation while(k > 0){ for( int i = 0; i < row; i++) { temp = arr[i][col-1]; // storing last column value to temp for(int j = col-1; j > 0; j--) { // reverse loop arr[i][j] = arr[i][j-1]; // shifting column value to right } arr[i][0] = temp; // storing temp value to array again } k--; // decementing k } System.out.println("After rotating the given matrix " ); // loop to print new matrix value for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } } }
Following is the output of the above program -
The given matrix: 7 2 1 3 6 1 3 7 4 9 8 0 8 0 1 2 After rotating the given matrix 2 1 3 7 1 3 7 6 9 8 0 4 0 1 2 8