Rotate a Matrix by 180 degree
Last Updated :
19 Aug, 2022
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.
C++
#include <bits/stdc++.h>
#define N 3
using namespace std;
void rotateMatrix( int mat[][N])
{
for ( int i = N - 1; i >= 0; i--) {
for ( int j = N - 1; j >= 0; j--)
printf ( "%d " , mat[i][j]);
printf ( "\n" );
}
}
int main()
{
int mat[N][N] = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
rotateMatrix(mat);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int N = 3 ;
static void rotateMatrix( int mat[][])
{
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();
}
}
public static void main(String[] args)
{
int [][] mat = { { 1 , 2 , 3 },
{ 4 , 5 , 6 },
{ 7 , 8 , 9 } };
rotateMatrix(mat);
}
}
|
Python3
N = 3 ;
def rotateMatrix(mat):
i = N - 1 ;
while (i > = 0 ):
j = N - 1 ;
while (j > = 0 ):
print (mat[i][j], end = " " );
j = j - 1 ;
print ();
i = i - 1 ;
mat = [[ 1 , 2 , 3 ],
[ 4 , 5 , 6 ],
[ 7 , 8 , 9 ]];
rotateMatrix(mat);
|
C#
using System;
class GFG {
static int N = 3;
static void rotateMatrix( int [, ] mat)
{
for ( int i = N - 1; i >= 0; i--) {
for ( int j = N - 1; j >= 0; j--)
Console.Write(mat[i, j] + " " );
Console.WriteLine();
}
}
static public void Main()
{
int [, ] mat = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
rotateMatrix(mat);
}
}
|
PHP
<?php
$N = 3;
function rotateMatrix( $mat )
{
global $N ;
for ( $i = $N - 1; $i >= 0; $i --)
{
for ( $j = $N - 1; $j >= 0; $j --)
echo $mat [ $i ][ $j ], " " ;
echo "\n" ;
}
}
$mat = array ( array (1, 2, 3),
array (4, 5, 6),
array (7, 8, 9));
rotateMatrix( $mat );
?>
|
Javascript
<script>
N = 3;
function rotateMatrix(mat)
{
for ( var i = N - 1; i >= 0; i--)
{
for ( var j = N - 1; j >= 0; j--)
document.write(mat[i][j] + " " );
document.write( "<br>" );
}
}
var mat = [ [ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ] ];
rotateMatrix(mat);
</script>
|
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
C++
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
void reverseColumns( int arr[R][C])
{
for ( int i = 0; i < C; i++)
for ( int j = 0, k = C - 1; j < k; j++, k--)
swap(arr[j][i], arr[k][i]);
}
void transpose( int arr[R][C])
{
for ( int i = 0; i < R; i++)
for ( int j = i; j < C; j++)
swap(arr[i][j], arr[j][i]);
}
void printMatrix( int arr[R][C])
{
for ( int i = 0; i < R; i++) {
for ( int j = 0; j < C; j++)
cout << arr[i][j] << " " ;
cout << '\n' ;
}
}
void rotate180( int arr[R][C])
{
transpose(arr);
reverseColumns(arr);
transpose(arr);
reverseColumns(arr);
}
int main()
{
int arr[R][C] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
rotate180(arr);
printMatrix(arr);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int R = 4 , C = 4 , t = 0 ;
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;
}
}
}
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;
}
}
}
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();
}
}
static void rotate180( int arr[][])
{
transpose(arr);
reverseColumns(arr);
transpose(arr);
reverseColumns(arr);
}
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);
}
}
|
C#
using System;
class GFG {
static int R = 4, C = 4, t = 0;
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;
}
}
}
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;
}
}
}
static void printMatrix( int [, ] arr)
{
for ( int i = 0; i < R; i++) {
for ( int j = 0; j < C; j++)
Console.Write(arr[i, j] + " " );
Console.WriteLine();
}
}
static void rotate180( int [, ] arr)
{
transpose(arr);
reverseColumns(arr);
transpose(arr);
reverseColumns(arr);
}
static public void Main()
{
int [, ] arr = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
rotate180(arr);
printMatrix(arr);
}
}
|
Python3
R = 4
C = 4
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
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
def printMatrix(arr):
for i in range (R):
for j in range (C):
print (arr[i][j], end = " " );
print ();
def rotate180(arr):
transpose(arr);
reverseColumns(arr);
transpose(arr);
reverseColumns(arr);
arr = [ [ 1 , 2 , 3 , 4 ],
[ 5 , 6 , 7 , 8 ],
[ 9 , 10 , 11 , 12 ],
[ 13 , 14 , 15 , 16 ] ];
rotate180(arr);
printMatrix(arr);
|
PHP
<?php
$R = 4;
$C = 4;
function reverseColumns(& $arr )
{
global $C ;
for ( $i = 0; $i < $C ; $i ++)
{
for ( $j = 0, $k = $C - 1; $j < $k ; $j ++, $k --)
{
$t = $arr [ $j ][ $i ];
$arr [ $j ][ $i ] = $arr [ $k ][ $i ];
$arr [ $k ][ $i ] = $t ;
}
}
}
function transpose(& $arr )
{
global $R , $C ;
for ( $i = 0; $i < $R ; $i ++)
{
for ( $j = $i ; $j < $C ; $j ++)
{
$t = $arr [ $i ][ $j ];
$arr [ $i ][ $j ] = $arr [ $j ][ $i ];
$arr [ $j ][ $i ] = $t ;
}
}
}
function printMatrix(& $arr )
{
global $R , $C ;
for ( $i = 0; $i < $R ; $i ++) {
for ( $j = 0; $j < $C ; $j ++)
{
echo $arr [ $i ][ $j ]. " " ;
}
echo "\n" ;
}
}
function rotate180(& $arr )
{
transpose( $arr );
reverseColumns( $arr );
transpose( $arr );
reverseColumns( $arr );
}
$arr = array ( array ( 1, 2, 3, 4 ),
array ( 5, 6, 7, 8 ),
array ( 9, 10, 11, 12 ),
array ( 13, 14, 15, 16 ) );
rotate180( $arr );
printMatrix( $arr );
return 0;
?>
|
Javascript
<script>
let R = 4, C = 4, t = 0;
function reverseColumns(arr)
{
for (let i = 0; i < C; i++) {
for (let j = 0, k = C - 1; j < k; j++, k--) {
t = arr[j][i];
arr[j][i] = arr[k][i];
arr[k][i] = t;
}
}
}
function transpose(arr)
{
for (let i = 0; i < R; i++) {
for (let j = i; j < C; j++) {
t = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = t;
}
}
}
function printMatrix(arr)
{
for (let i = 0; i < R; i++) {
for (let j = 0; j < C; j++)
document.write(arr[i][j] + " " );
document.write( "<br>" );
}
}
function rotate180(arr)
{
transpose(arr);
reverseColumns(arr);
transpose(arr);
reverseColumns(arr);
}
let arr = [ [ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ],
[9, 10, 11, 12 ],
[13, 14, 15, 16 ] ];
rotate180(arr);
printMatrix(arr);
</script>
|
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.
C++
#include <bits/stdc++.h>
using namespace std;
void reverseRow(vector<vector< int >>& data,
int index)
{
int cols = data[index].size();
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;
}
}
void printMatrix(vector<vector< int >>& data)
{
for ( int i = 0; i < data.size(); i++)
{
for ( int j = 0; j < data[i].size(); j++)
{
cout << data[i][j] << " " ;
}
cout << endl;
}
}
void rotateMatrix180(vector<vector< int >>& data)
{
int rows = data.size();
int cols = data[0].size();
if (rows % 2 != 0)
{
reverseRow(data, data.size() / 2);
}
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;
}
}
}
int main()
{
vector<vector< 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 } };
rotateMatrix180(data);
printMatrix(data);
return 0;
}
|
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 ) {
reverseRow(data, data.length / 2 );
}
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 }
};
rotateMatrix180(data);
printMatrix(data);
}
}
|
Python3
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
def printMatrix(data):
for i in range ( len (data)):
for j in range ( len (data[ 0 ])):
print (data[i][j], end = ' ' )
print ()
def rotateMatrix(data):
rows = len (data)
cols = len (data[ 0 ])
if (rows % 2 ):
data = reverseRow(data, len (data) / / 2 )
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
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 ] ]
data = rotateMatrix(data)
printMatrix(data)
|
C#
using System;
class GFG
{
private static void reverseRow( int [, ] data, int index)
{
int cols = data.GetLength(1);
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;
}
}
private static void printMatrix( int [, ] data)
{
for ( int i = 0; i < data.GetLength(0); i++)
{
for ( int j = 0; j < data.GetLength(1); j++)
{
Console.Write(data[i, j] + " " );
}
Console.WriteLine();
}
}
private static void rotateMatrix180( int [, ] data)
{
int rows = data.GetLength(0);
int cols = data.GetLength(1);
if (rows % 2 != 0)
{
reverseRow(data, data.GetLength(0) / 2);
}
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()
{
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 } };
rotateMatrix180(data);
printMatrix(data);
}
}
|
Javascript
<script>
function reverseRow(data,index)
{
let cols = data[index].length;
for (let i = 0; i < cols / 2; i++)
{
let temp = data[index][i];
data[index][i] = data[index][cols - i - 1];
data[index][cols - i - 1] = temp;
}
}
function printMatrix(data)
{
for (let i = 0; i < data.length; i++)
{
for (let j = 0; j < data[i].length; j++)
{
document.write(data[i][j] + " " );
}
document.write( "<br>" );
}
}
function rotateMatrix180(data)
{
let rows = data.length;
let cols = data[0].length;
if (rows % 2 != 0)
{
reverseRow(data, Math.floor(data.length / 2));
}
for (let i = 0; i <= (rows/2) - 1; i++)
{
for (let j = 0; j < cols; j++)
{
let temp = data[i][j];
data[i][j] = data[rows - i - 1][cols - j - 1];
data[rows - i - 1][cols - j - 1] = temp;
}
}
}
let 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 ] ];
rotateMatrix180(data);
printMatrix(data);
</script>
|
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), since no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...