GeeksforGeeks App
Open App
Browser
Continue

# 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 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
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 the 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.

## Python3

 `# Python3 program to``# rotate a matrix by``# 180 degrees``N ``=` `3` `# Function to Rotate``# the matrix by 180 degree`  `def` `rotateMatrix(mat):` `    ``# Simply print from``    ``# last cell to first cell.``    ``i ``=` `N ``-` `1``    ``while``(i >``=` `0``):``        ``j ``=` `N ``-` `1``        ``while``(j >``=` `0``):``            ``print``(mat[i][j], end``=``" "``)``            ``j ``=` `j ``-` `1``        ``print``()``        ``i ``=` `i ``-` `1`  `# Driven code``mat ``=` `[[``1``, ``2``, ``3``],``       ``[``4``, ``5``, ``6``],``       ``[``7``, ``8``, ``9``]]``rotateMatrix(mat)`

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```

## Python3

 `# Python3 program for left rotation of matrix by 180` `R ``=` `4``C ``=` `4` `# Function to rotate the matrix by 180 degree`  `def` `reverseColumns(arr):``    ``for` `i ``in` `range``(C):``        ``j ``=` `0``        ``k ``=` `C``-``1``        ``while` `j < k:``            ``t ``=` `arr[j][i]``            ``arr[j][i] ``=` `arr[k][i]``            ``arr[k][i] ``=` `t``            ``j ``+``=` `1``            ``k ``-``=` `1` `# Function for transpose of matrix`  `def` `transpose(arr):``    ``for` `i ``in` `range``(R):``        ``for` `j ``in` `range``(i, C):``            ``t ``=` `arr[i][j]``            ``arr[i][j] ``=` `arr[j][i]``            ``arr[j][i] ``=` `t` `# Function for display the matrix`  `def` `printMatrix(arr):``    ``for` `i ``in` `range``(R):``        ``for` `j ``in` `range``(C):``            ``print``(arr[i][j], end``=``" "``)``        ``print``()` `# Function to anticlockwise rotate matrix``# by 180 degree`  `def` `rotate180(arr):``    ``transpose(arr)``    ``reverseColumns(arr)``    ``transpose(arr)``    ``reverseColumns(arr)`  `# Driven code``arr ``=` `[[``1``, ``2``, ``3``, ``4``],``       ``[``5``, ``6``, ``7``, ``8``],``       ``[``9``, ``10``, ``11``, ``12``],``       ``[``13``, ``14``, ``15``, ``16``]]``rotate180(arr)``printMatrix(arr)`

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.

## Python3

 `# Reverse Row at specified index in the matrix``def` `reverseRow(data, index):` `    ``cols ``=` `len``(data[index])``    ``for` `i ``in` `range``(cols ``/``/` `2``):``        ``temp ``=` `data[index][i]``        ``data[index][i] ``=` `data[index][cols ``-` `i ``-` `1``]``        ``data[index][cols ``-` `i ``-` `1``] ``=` `temp` `    ``return` `data` `# Print Matrix data`  `def` `printMatrix(data):` `    ``for` `i ``in` `range``(``len``(data)):``        ``for` `j ``in` `range``(``len``(data[``0``])):``            ``print``(data[i][j], end``=``' '``)` `        ``print``()` `# Rotate Matrix by 180 degrees`  `def` `rotateMatrix(data):` `    ``rows ``=` `len``(data)``    ``cols ``=` `len``(data[``0``])` `    ``if` `(rows ``%` `2``):` `        ``# If N is odd reverse the middle``        ``# row in the matrix``        ``data ``=` `reverseRow(data, ``len``(data) ``/``/` `2``)` `        ``# Swap the value of matrix [i][j] with``        ``# [rows - i - 1][cols - j - 1] for half``        ``# the rows size.``        ``for` `i ``in` `range``(rows ``/``/` `2``):``            ``for` `j ``in` `range``(cols):``                ``temp ``=` `data[i][j]``                ``data[i][j] ``=` `data[rows ``-` `i ``-` `1``][cols ``-` `j ``-` `1``]``                ``data[rows ``-` `i ``-` `1``][cols ``-` `j ``-` `1``] ``=` `temp` `        ``return` `data`  `# Driver Code``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``data ``=` `rotateMatrix(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)

## Python3

 `# Rotate Matrix by 180 degrees``def` `rotateMatrix(data):` `    ``rows ``=` `len``(data)``    ``cols ``=` `len``(data[``0``])``    ``# Reversing all rows``    ``for` `i ``in` `range``(``len``(data)):``        ``data[i] ``=` `data[i][::``-``1``]``    ``# Reversing all rows of the matrix``    ``data ``=` `data[::``-``1``]``    ``for` `i ``in` `range``(rows):``        ``for` `j ``in` `range``(cols):``            ``print``(data[i][j], end``=``' '``)``        ``print``()`  `# Driver Code``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``rotateMatrix(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
```

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

My Personal Notes arrow_drop_up