Open In App

Java Program for Rotate a Matrix by 180 degree

Last Updated : 13 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a square matrix, the task is that we turn it by 180 degrees in an anti-clockwise direction without using any extra space. 

Examples : 

Input :  1  2  3
         4  5  6
         7  8  9
Output : 9 8 7 
         6 5 4 
         3 2 1

Input :  1 2 3 4 
         5 6 7 8 
         9 0 1 2 
         3 4 5 6 
Output : 6 5 4 3 
         2 1 0 9 
         8 7 6 5 
         4 3 2 1

Method: 1 (Only prints rotated matrix) 
The solution of this problem is that to rotate a matrix by 180 degrees we can easily follow that step 

Matrix =  a00 a01 a02
          a10 a11 a12
          a20 a21 a22

when we rotate it by 90 degree
then matrix is
Matrix = a02 a12 a22
         a01 a11 a21
         a00 a10 a20
  
when we rotate it by again 90 
degree then matrix is 
Matrix = a22 a21 a20
         a12 a11 a10
         a02 a01 a00 

From the above illustration, we get that simply to rotate the matrix by 180 degrees then we will have to print the given matrix in a reverse manner.

Java




// Java program to rotate a
// matrix by 180 degrees
import java.util.*;
  
class GFG {
    static int N = 3;
  
    // Function to Rotate the
    // matrix by 180 degree
    static void rotateMatrix(int mat[][])
    {
  
        // Simply print from last
        // cell to first cell.
        for (int i = N - 1; i >= 0; i--) {
            for (int j = N - 1; j >= 0; j--)
                System.out.print(mat[i][j] + " ");
  
            System.out.println();
        }
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int[][] mat = { { 1, 2, 3 },
                        { 4, 5, 6 },
                        { 7, 8, 9 } };
  
        rotateMatrix(mat);
    }
}
  
// This code is contributed by ChitraNayal


Output : 

 9 8 7 
 6 5 4 
 3 2 1  

Time complexity: O(N*N) 
Auxiliary Space: O(1)

Method : 2(In-place rotation) 
There are four steps : 
1- Find transpose of a matrix. 
2- Reverse columns of the transpose. 
3- Find transpose of a matrix. 
4- Reverse columns of the transpose

Let the given matrix be
1  2  3  4
5  6  7  8
9  10 11 12
13 14 15 16

First we find transpose.
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16

Then we reverse elements of every column.
4 8 12 16
3 7 11 15
2 6 10 14
1 5  9 13

then transpose again 
4 3 2 1 
8 7 6 5 
12 11 10 9
16 15 14 13 

Then we reverse elements of every column again
16 15 14 13 
12 11 10 9 
8 7 6 5 
4 3 2 1

Java




// Java program for left
// rotation of matrix by 180
import java.util.*;
  
class GFG {
    static int R = 4, C = 4, t = 0;
  
    // Function to rotate the
    // matrix by 180 degree
    static void reverseColumns(int arr[][])
    {
        for (int i = 0; i < C; i++) {
            for (int j = 0, k = C - 1; j < k; j++, k--) {
                t = arr[j][i];
                arr[j][i] = arr[k][i];
                arr[k][i] = t;
            }
        }
    }
  
    // Function for transpose of matrix
    static void transpose(int arr[][])
    {
        for (int i = 0; i < R; i++) {
            for (int j = i; j < C; j++) {
                t = arr[i][j];
                arr[i][j] = arr[j][i];
                arr[j][i] = t;
            }
        }
    }
  
    // Function for display the matrix
    static void printMatrix(int arr[][])
    {
        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++)
                System.out.print(arr[i][j] + " ");
            System.out.println();
        }
    }
  
    // Function to anticlockwise
    // rotate matrix by 180 degree
    static void rotate180(int arr[][])
    {
        transpose(arr);
        reverseColumns(arr);
        transpose(arr);
        reverseColumns(arr);
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int[][] arr = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12 },
                        { 13, 14, 15, 16 } };
  
        rotate180(arr);
        printMatrix(arr);
    }
}
  
// This code is contributed by ChitraNayal


Output : 

 16 15 14 13 
 12 11 10 9 
 8 7 6 5 
 4 3 2 1

Time complexity : O(R*C) 
Auxiliary Space : O(1)
In the code above, the transpose of the matrix has to be found twice, and also, columns have to be reversed twice. 
So, we can have a better solution.

Method : 3 (Position swapping)
Here, we swap the values in the respective positions. 

Java




public class GFG {
       /**
     * Reverse Row at specified index in the matrix
     * @param data matrix
     * @param index row index
     */
    private static void reverseRow(int[][] data, int index) {
        int cols = data[index].length;
        for (int i = 0; i < cols / 2; i++) {
            int temp = data[index][i];
            data[index][i] = data[index][cols - i - 1];
            data[index][cols - i - 1] = temp;
        }
    }
  
    /**
     * Print Matrix data
     * @param data matrix
     */
    private static void printMatrix(int[][] data) {
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                System.out.print(data[i][j] + " ");
            }
            System.out.println("");
        }
    }
  
    /**
     * Rotate Matrix by 180 degrees
     * @param data matrix
     */
    private static void rotateMatrix180(int[][] data) {
        int rows = data.length;
        int cols = data[0].length;
  
        if (rows % 2 != 0) {
            //If N is odd reverse the middle row in the matrix
            reverseRow(data, data.length / 2);
        }
  
        //Swap the value of matrix [i][j] with [rows - i - 1][cols - j - 1] for half the rows size. 
        for (int i = 0; i <= (rows/2) - 1; i++) {
            for (int j = 0; j < cols; j++) {
                int temp = data[i][j];
                data[i][j] = data[rows - i - 1][cols - j - 1];
                data[rows - i - 1][cols - j - 1] = temp;
            }
        }
    }
  
    public static void main(String[] args) {
        int[][] data = {
                {1, 2, 3, 4, 5},
                {6, 7, 8, 9, 10},
                {11, 12, 13, 14, 15},
                {16, 17, 18, 19, 20},
                {21, 22, 23, 24, 25}
        };
  
        //Rotate Matrix
        rotateMatrix180(data);
  
        //Print Matrix
        printMatrix(data);
    }
}


Output : 

25 24 23 22 21 
20 19 18 17 16 
15 14 13 12 11 
10 9 8 7 6 
5 4 3 2 1 

Time complexity : O(R*C) 
Auxiliary Space : O(1)
 

Please refer complete article on Rotate a Matrix by 180 degree for more details!



Similar Reads

C++ Program to Rotate a Matrix by 180 degree
Given a square matrix, the task is that we turn it by 180 degrees in an anti-clockwise direction without using any extra space.  Examples :  Input : 1 2 3 4 5 6 7 8 9 Output : 9 8 7 6 5 4 3 2 1 Input : 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 Output : 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 Method: 1 (Only prints rotated matrix) The solution of this problem is that
6 min read
Python Program for Rotate a Matrix by 180 degree
Given a square matrix, the task is that turn it by 180 degrees in an anti-clockwise direction without using any extra space. Examples : Input: 1 2 3 4 5 6 7 8 9 Output: 9 8 7 6 5 4 3 2 1Input: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 Output: 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1Python Program for Rotate a Matrix by 180 degreeThe solution of this problem is that t
5 min read
Javascript Program for Rotate a Matrix by 180 degree
Given a square matrix, the task is that we turn it by 180 degrees in an anti-clockwise direction without using any extra space.  Examples :  Input : 1 2 3 4 5 6 7 8 9 Output : 9 8 7 6 5 4 3 2 1 Input : 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 Output : 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 Method: 1 (Only prints rotated matrix) The solution of this problem is that
6 min read
Rotate a Matrix by 180 degree
Given a square matrix, the task is that we turn it by 180 degrees in an anti-clockwise direction without using any extra space. Examples : Input : 1 2 3 4 5 6 7 8 9 Output : 9 8 7 6 5 4 3 2 1 Input : 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 Output : 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 Method: 1 (Only prints rotated matrix) The solution of this problem is that t
19 min read
Java Program For Printing 180 Degree Rotation of Simple Half Left Pyramid
We can utilize 'for' and 'while' loops to print a 180-degree rotation of a simple half-left pyramid in Java as follows: Example of 180-degree Rotation of Simple Half-Left PyramidRows = 5Output : * * * * * * * * * * * * * * * Methods For Printing 180-Degree Rotation of Simple Half-Left PyramidThere are certain methods to Print the 180-rotated Simple
3 min read
Reflection of a point at 180 degree rotation of another point
Given two points coordinates (x1, y1) and (x2, y2)on 2D plane. The task is to find the reflection of (x1, y1) at 180 degree rotation of (x2, y2).Examples: Input : x1 = 0, y1 = 0, x2 = 1, y2 = 1 Output : (2, 2) Input : x1 = 1, y1 = 1, x2 = 2, y2 = 2 Output : (3, 3) Let the reflection point of point (x1, y1) about (x2, y2) be (x', y'). For (x', y') b
4 min read
Count numbers in given range which are symmetrical when rotated 180 degree clockwise
Given a range [L, R), the task is to count the numbers that remain unchanged when rotated 180 degrees clockwise. Examples: Input: L = 0, R =10 Output: 3Explanation: Here between 0 and 10 ( 10 exclusive) the desired numbers are: 0, 1, and 8. Notice 6 and 9 are not included because, when 6 is rotated 180 degree it's value changes to 9.Same happens fo
10 min read
Rotate a matrix by 90 degree in clockwise direction without using any extra space
Given a square matrix, turn it by 90 degrees in a clockwise direction without using any extra space. Examples: Input: 1 2 3 4 5 6 7 8 9 Output: 7 4 1 8 5 2 9 6 3 Input: 1 2 3 4 Output: 3 1 4 2 Method 1 Approach: The approach is similar to Inplace rotate square matrix by 90 degrees | Set 1. The only thing that is different is to print the elements o
25 min read
Rotate a matrix clockwise by 90 degree without using any extra space | Set 3
Given a rectangular matrix mat[][] with N rows and M columns, the task is to rotate the matrix by 90 degrees in a clockwise direction without using extra space. Examples: Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}Output: 10 7 4 1 11 8 5 2 12 9 6 3 Input: mat[][] = {{1, 2, 3, 4, 5, 6}, {7, 8, 9, 10, 11, 12}}Output: 7 1 8 2 9 3
10 min read
Rotate a matrix by 90 degree without using any extra space | Set 2
Given a square matrix, turn it by 90 degrees in anti-clockwise direction without using any extra space. Examples: Input: 1 2 3 4 5 6 7 8 9Output: 3 6 9 2 5 8 1 4 7 Rotated the input matrix by90 degrees in anti-clockwise direction.Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 4 8 12 16 3 7 11 15 2 6 10 14 1 5 9 13Rotated the input matrix by9
25 min read