Rotate a Matrix by 180 degrees in Java
The problem statement is to rotate the given square matrix by 180 degrees in the anti-clockwise direction.
Example-1
Input
![Rotate a Matrix by 180 degrees in Java](https://static.tutorialandexample.com/java/rotate-a-matrix-by-180-degrees-in-java1.png)
Output
![Rotate a Matrix by 180 degrees in Java](https://static.tutorialandexample.com/java/rotate-a-matrix-by-180-degrees-in-java2.png)
Explanation
In order to rotate the above-mentioned square matrix by 180 degrees, we must simply access it in reverse order, which will provide us with the required output. The square array must be accessed from the last element of the last row to the first element of the first row in reverse order.
Example-2
Input
![Rotate a Matrix by 180 degrees in Java](https://static.tutorialandexample.com/java/rotate-a-matrix-by-180-degrees-in-java3.png)
Output
![Rotate a Matrix by 180 degrees in Java](https://static.tutorialandexample.com/java/rotate-a-matrix-by-180-degrees-in-java4.png)
Explanation
In the above-mentioned example, the 5x5 input matrix will be reversed in order to get the output. The last row's last element will be accessed first, and the first row's first element will be accessed last, and the order of accessing values will be in reverse. Consider printing the elements of the array from the last row to the first; then, the required output will be generated.
Example-3
Input
![Rotate a Matrix by 180 degrees in Java](https://static.tutorialandexample.com/java/rotate-a-matrix-by-180-degrees-in-java5.png)
Output
![Rotate a Matrix by 180 degrees in Java](https://static.tutorialandexample.com/java/rotate-a-matrix-by-180-degrees-in-java6.png)
Explanation
In this 2x2 square matrix, the matrix will be rotated 180 degrees by printing the last row elements in reverse order, then coming to the last second row until the first row is printed too.
Simple Approach: Printing the Rotated Matrix
In this approach or technique, the rotated matrix can be easily generated by printing the given input array in reverse order. Firstly, the last row will be printed in the reverse order. Then the last second row will be printed in the reverse order. This reverse printing procedure will be continued till the first row of the input array has been printed.
Input_Matrix = i[0][0] i[0][1] i[0][2]
i[1][0] i[1][1] i[1][2] i[2][0] i[2][1] i[2][2]
The matrix can be rotated by 180 degrees by rotating it 90 degrees twice. After rotating the matrix by 90 degrees, the values will be:
Input_Matrix = i[0][2] i[1][2] i[2][2]
i[0][1] i[1][1] i[2][1] i[0][0] i[1][0] i[2][0]
The input matrix must be rotated again by 90 degrees to generate the desired output. The values of the input matrix after rotating by 90 degrees will be:
Input_Matrix = i[2][2] i[2][1] i[2][0]
i[1][2] i[1][1] i[1][0] i[0][2] i[0][1] i[0][0]
After rotating the input matrix twice by 90 degrees, it will generate the required output. Using the above-mentioned simple approach, we can rotate a matrix by 180 degrees.
Filename: RotatedMatrix.java
// This is a Java program to rotate a given square matrix by 180 degrees. // This program will follow the simple approach by printing the square matrix in reverse order. import java.util.*; class RotatedMatrix{ static int num = 3; // This function is used to rotate the given matrix by 180 degrees. static void Rotate_Matrix(int Input_Matrix[][]) { // These for loops are used to access the matrix elements in reverse order. for (int a = num - 1; a >= 0; a--) { for (int b = num - 1; b >= 0; b--) System.out.print(Input_Matrix[a][b] + " "); System.out.println(); } } //Main method public static void main(String[] args) { //Initialization of Input Matrix int[][] Input_Matrix = { {1,3,5}, {2,4,6}, {3,5,7} }; System.out.println(“The rotated matrix generated is: ”); Rotate_Matrix(Input_Matrix); } } // This code is written by R K Eshwar
Output
The rotated matrix generated is:
![Rotate a Matrix by 180 degrees in Java](https://static.tutorialandexample.com/java/rotate-a-matrix-by-180-degrees-in-java7.png)
Complexity Analysis: The time complexity of the above-mentioned approach of rotating a matrix by 180 degrees is O(n*n), where 'n' is the total number of elements present in the matrix. At the same time, the space complexity of the above-mentioned program will be constant or O(1).
Points to remember:
In the above-mentioned approach, the input square matrix will be simply printed in reverse order to generate the 180-degree rotated matrix. The original input matrix will remain unchanged.
Approach: In-Place Rotation:
In this approach, the matrix will be rotated by 180 degrees using in-place rotation. There are majorly four steps utilized in the in-place rotation approach or technique.
Step-1: The first step of the above-mentioned approach would be finding the transpose of the input square matrix.
Step-2: The second step would be reversing the columns of the previously obtained transpose matrix.
Step-3: The third step of the above-mentioned approach would be finding the transpose of the matrix again.
Step-4: The final step involved in generating the required output will be reversing the columns present in the transpose matrix.
Example-1
Input Matrix:
![Rotate a Matrix by 180 degrees in Java](https://static.tutorialandexample.com/java/rotate-a-matrix-by-180-degrees-in-java8.png)
The first step involves generating the transpose of the input matrix. The transpose of the input square matrix will be:
Transpose Matrix:
![Rotate a Matrix by 180 degrees in Java](https://static.tutorialandexample.com/java/rotate-a-matrix-by-180-degrees-in-java9.png)
The following step of the above-mentioned approach instructs to reverse every individual value of all the columns. The intermediate matrix generated would be:
Reverse Matrix:
![Rotate a Matrix by 180 degrees in Java](https://static.tutorialandexample.com/java/rotate-a-matrix-by-180-degrees-in-java10.png)
The next step will be transposing the intermediate reverse matrix to generate the result.
Transpose Matrix:
![Rotate a Matrix by 180 degrees in Java](https://static.tutorialandexample.com/java/rotate-a-matrix-by-180-degrees-in-java11.png)
The final step of this procedure or approach is to reverse all the columns to generate the final result.
Result:
![Rotate a Matrix by 180 degrees in Java](https://static.tutorialandexample.com/java/rotate-a-matrix-by-180-degrees-in-java12.png)
This is the required resultant matrix.
Filename: RotatedMatrix1.java
// The Java program for rotating the given matrix by 180 degrees // This method uses in-place rotation appraoch. import java.util.*; class RotatedMatrix { static int row = 4, col = 4, temp = 0; //Function used to rotate a matrix by 180 degrees. static void columns_reversed(int matrix[][]) { for (int i = 0; i < col; i++) { for (int j = 0, k = col - 1; j < k; j++, k--) { temp = matrix[j][i]; matrix[j][i] = matrix[k][i]; matrix[k][i] = temp; } } } // Function used for finding tranpose of a matrix. static void transpose_matrix(int matrix[][]) { for (int i = 0; i < row; i++) { for (int j = i; j < col; j++) { temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } } // The function used to display the contents of a matrix. static void display_matrix(int matrix[][]) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) System.out.print(matrix[i][j] + " "); System.out.println(); } } // This function rotates the matrix in anti-clockwise direction. static void rotate_by_180(int matrix[][]) { transpose_matrix(matrix); columns_reversed(matrix); transpose_matrix(matrix); columns_reversed(matrix); } public static void main(String[] args) { int[][] matrix = { {4,3,2,1} , {5,4,3,2} , {1,2,3,4} , {2,3,4,5} }; rotate_180(matrix); display_matrix(matrix); } } // This program was written by R K Eshwar
Output
![Rotate a Matrix by 180 degrees in Java](https://static.tutorialandexample.com/java/rotate-a-matrix-by-180-degrees-in-java13.png)
Complexity Analysis: The above-mentioned approach takes O(R*C) time complexity. R stands for the number of rows, and C stands for the number of columns. At the same time, the above-mentioned program takes constant space complexity.
Points to remember:
In the above-mentioned approach or program, the transpose must be calculated two times. At the same time, the reversal of the matrix occurs twice. This minimizes the efficiency of the program.
Approach: Swapping the positions:
Using this approach, the values of the elements in the matrix will be swapped or changed, and the required rotated matrix will be generated.
Filename: RotatedMatrix3.java
public class RotatedMatrix{ private static void reverse_row(int[][] matrix, int pos) { int c = matrix[pos].length; for (int i = 0; i < c / 2; i++) { int temp = matrix[pos][i]; matrix[pos][i] = matrix[pos][c - i - 1]; matrix[pos][c - i - 1] = temp; } } //This method will print the values of the matrix private static void display_matrix(int[][] matrix) { for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(""); } } //This method will rotate the matrix by 180 degrees. private static void rotate_by_180(int[][] matrix) { int r = matrix.length; int c = matrix[0].length; if (r % 2 != 0) { reverse_row(matrix, matrix.length / 2); } for (int i = 0; i <= (r/2) - 1; i++) { for (int j = 0; j < c; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[r - i - 1][c - j - 1]; matrix[r - i - 1][c - j - 1] = temp; } } } public static void main(String[] args) { int[][] matrix = { {4,3,2,1} , {5,4,3,2} , {1,2,3,4} , {2,3,4,5} }; rotate_by_180(matrix); display_matrix(matrix); } }
Output:
![Rotate a Matrix by 180 degrees in Java](https://static.tutorialandexample.com/java/rotate-a-matrix-by-180-degrees-in-java14.png)
Complexity Analysis: The above-mentioned approach takes O(R*C) time complexity. Here, R stands for the number of rows, and C stands for the number of columns. Whereas, as no extra space has been utilized by this approach, it takes O(1) space complexity.