# Rotate A Matrix By 180 Degree in Java

In this section, we will discuss how to rotate a matrix by 180 degree in Java. In this problem, a square matrix is given, and we have to rotate it by 1800.

Example 1:

Input:

4 6 7 8

9 3 2 1

9 0 4 5

8 0 3 2

Output:

2 3 0 8

5 4 0 9

1 2 3 9

8 7 6 4

## Approach to Rotate a Matrix by 180 Degree

There are various approaches to solve the problem but, in this section, we will discuss the following approaches:

• Naive Approach
• Using Transpose Matrix
• By Swapping of Positions

Let's start with the simplest approach first.

### Naive Approach

Consider the above examples. We observe that we are printing the rows of the input matrix in the reverse order starting from the bottom-most row and going upwards.

For example, if an input array has the 5 rows, then the 5th row is printed first. After that, the 4th row is printed, then the 3rd row is printed, and so on.

Let's see the implementation of the simplest approach.

FileName: RotateMatrix.java

Output:

```For the following matrix:
4 6 7 8
9 3 2 1
9 0 4 5
8 0 3 2

The rotated matrix at 180 degree is:
2 3 0 8
5 4 0 9
1 2 3 9
8 7 6 4

For the following matrix:
4 7 8 6 7
9 2 1 9 1
9 0 5 4 3
5 6 7 2 1
8 9 0 5 1

The rotated matrix at 180 degree is:
1 5 0 9 8
1 2 7 6 5
3 4 5 0 9
1 9 1 2 9
7 6 8 7 4
```

### Using Transpose Matrix

The following algorithm depicts how one can use the concept of the transpose of the matrix to solve the problem.

Step 1: Compute the transpose of the given matrix.

Step 2: Reverse the column of the transposed matrix.

Step 3: After reversing the column in the previous step, find the transpose of the matrix again.

Step 4: Reverse the column of the matrix found in step 3.

For example, if we have an input matrix as:

14 27 38 46 57

79 52 31 39 11

99 80 15 24 43

45 56 97 82 51

78 79 60 25 41

Then its transpose will be

14 79 99 45 78

27 52 80 56 79

38 31 15 97 60

46 39 24 82 25

57 11 43 51 41

Now, we will reverse its columns

57 11 43 51 41

46 39 24 82 25

38 31 15 97 60

27 52 80 56 79

14 79 99 45 78

Again, we will transpose the matrix.

57 46 38 27 14

11 39 31 52 79

43 24 15 80 99

51 82 97 56 45

41 25 60 79 78

Again, we will reverse its columns.

41 25 60 79 78

51 82 97 56 45

43 24 15 80 99

11 39 31 52 79

57 46 38 27 14

The following program depicts the implementation of the above approach.

FileName: RotateMatrix1.java

Output:

```For the following matrix:
4 6 7 8
9 3 2 1
9 0 4 5
8 0 3 2

The rotated matrix at 180 degree is:
2 3 0 8
5 4 0 9
1 2 3 9
8 7 6 4

For the following matrix:
4 7 8 6 7
9 2 1 9 1
9 0 5 4 3
5 6 7 2 1
8 9 0 5 1

The rotated matrix at 180 degree is:
1 5 0 9 8
1 2 7 6 5
3 4 5 0 9
1 9 1 2 9
7 6 8 7 4
```

### By Swapping of Positions

As we can see in the above approach, the transpose of the matrix has to be found twice. Also, the reversal of the columns has to be found twice. Therefore, we can do some optimization to get a better solution.

In this approach, we will swap the values in the different positions. Observe the following program.

FileName: RotateMatrix2.java

Output:

```For the following matrix:
4 6 7 8
9 3 2 1
9 0 4 5
8 0 3 2

The rotated matrix at 180 degree is:
2 3 0 8
5 4 0 9
1 2 3 9
8 7 6 4

For the following matrix:
4 7 8 6 7
9 2 1 9 1
9 0 5 4 3
5 6 7 2 1
8 9 0 5 1

The rotated matrix at 180 degree is:
1 5 0 9 8
1 2 7 6 5
3 4 5 0 9
1 9 1 2 9
7 6 8 7 4
```

Complexity Analysis

In the above programs, we are traversing each row and column of the matrix. It leads to the time complexity of O(R * C), where R is the row size of the input matrix and C is the column size of the input matrix.

Note that we are not using any auxiliary space to store the data; for swapping the data, we are using the same input array that leads to the space complexity of O(1).

## Help Others, Please Share   