Rotate a Matrix by 180 degree
The problem involves rotating a given matrix by 180 degrees. This means that the elements of the matrix need to be rearranged in a way that the matrix is flipped both horizontally and vertically, resulting in a 180-degree rotation.
Example
Consider the following matrix:
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
After rotating the matrix by 180 degrees, it becomes:
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
Idea to Solve the Problem
The idea to rotate a matrix by 180 degrees is to swap elements symmetrically across the center vertical line. To do this, we can iterate through the matrix up to its midpoint row (if the number of rows is odd, include the middle row), and within that, iterate up to the middle column. For each element, we swap it with its symmetric counterpart (mirror element) in the matrix.
Algorithm
- Define a function
rotate_matrix
that takes a 2D matrix as a parameter. - Initialize variables
temp
,row
, andcol
. - Define
row_size
as half of the number of rows. - If the number of rows is odd, increment
row_size
by 1. - Loop through rows up to
row_size
:- If the current row is the middle row (in case of odd rows), set
col_size
to half of the number of columns. - Else, set
col_size
to the number of columns. - Loop through columns up to
col_size
:- Swap the element at
[i][j]
with the element at[row-i][col-j]
(mirror element).
- Swap the element at
- If the current row is the middle row (in case of odd rows), set
- Define a function
show_data
to display the matrix. - In the
main
function:- Define the matrix.
- Display the original matrix using the
show_data
function. - Rotate the matrix using the
rotate_matrix
function. - Display the rotated matrix using the
show_data
function.
Pseudocode
rotate_matrix(matrix):
temp = 0
row = ROW - 1
col = COL - 1
row_size = ROW / 2
if ROW is odd:
increment row_size by 1
for i from 0 to row_size:
if i is equal to row_size:
set col_size to COL / 2
else:
set col_size to COL
for j from 0 to col_size:
swap matrix[i][j] with matrix[row-i][col-j]
show_data(matrix):
for i from 0 to ROW:
for j from 0 to COL:
print matrix[i][j]
print newline
main:
matrix = ... // Define the matrix
show_data(matrix)
rotate_matrix(matrix)
show_data(matrix)
Code Solution
//C Program
//Rotate a Matrix by 180 degree
#include<stdio.h>
#define ROW 5
#define COL 5
//rotate matrix elements in 180 degree
void rotate_matrix(int matrix[][COL])
{
int temp=0;
int row=ROW-1,col=COL-1;
int row_size = (ROW/2);
int col_size = COL;
if(ROW%2==1)
{
//When in case number of rows are Odd size
row_size = (ROW/2)+1;
}
for (int i = 0; i < row_size; ++i)
{
if(ROW/2 == i)
{
//When in case number of rows are Odd size
//In This case reverse middle row element
col_size = COL/2;
}
else
{
col_size = COL;
}
for (int j = 0; j < col_size; ++j)
{
//Swap element
temp=matrix[i][j];
matrix[i][j]=matrix[row-i][col-j];
matrix[row-i][col-j]=temp;
}
}
}
//Display matrix
void show_data(int matrix[][COL])
{
for (int i = 0; i < ROW; ++i)
{
for (int j = 0; j < COL; ++j)
{
printf("%3d",matrix[i][j] );
}
printf("\n");
}
printf("\n");
}
int main()
{
int matrix[][COL]={
{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}
};
show_data(matrix);
rotate_matrix(matrix);
show_data(matrix);
return 0;
}
Output
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
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
/*
C++ Program
Rotate a Matrix by 180 degree
*/
#include<iostream>
#define ROW 5
#define COL 5
using namespace std;
class MyMatrix {
public:
int rows;
int cols;
MyMatrix() {
//Get matrix size
this->rows = ROW;
this->cols = COL;
}
//rotate matrix elements in 180 degree
void rotate_matrix(int matrix[][COL]) {
int temp = 0;
int row = this->rows - 1, col = this->cols - 1;
int row_size = (this->rows / 2);
int col_size = this->cols;
if (this->rows % 2 == 1) {
//When in case number of rows are Odd size
row_size = (this->rows / 2) + 1;
}
for (int i = 0; i < row_size; ++i) {
if (this->rows / 2 == i) {
//When in case number of rows are Odd size
//In This case reverse middle row element
col_size = this->cols / 2;
} else {
col_size = this->cols;
}
for (int j = 0; j < col_size; ++j) {
//Swap element
temp = matrix[i][j];
matrix[i][j] = matrix[row - i][col - j];
matrix[row - i][col - j] = temp;
}
}
}
//Display matrix
void show_data(int matrix[][COL]) {
for (int i = 0; i < this->rows; ++i) {
for (int j = 0; j < this->cols; ++j) {
cout << " " << matrix[i][j];
}
cout << "\n";
}
cout << "\n";
}
};
int main() {
int matrix[][COL] = {
{
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
}
};
MyMatrix obj ;
obj.show_data(matrix);
obj.rotate_matrix(matrix);
obj.show_data(matrix);
return 0;
}
Output
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
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
/*
Java Program
Rotate a Matrix by 180 degree
*/
public class MyMatrix {
int rows;
int cols;
MyMatrix(int[][] matrix) {
//Get matrix size
this.rows = matrix.length;
this.cols = matrix[0].length;
}
//rotate matrix elements in 180 degree
void rotate_matrix(int [][]matrix)
{
int temp=0;
int row=this.rows-1,col=this.cols-1;
int row_size = (this.rows/2);
int col_size = this.cols;
if(this.rows%2==1)
{
//When in case number of rows are Odd size
row_size = (this.rows/2)+1;
}
for (int i = 0; i < row_size; ++i)
{
if(this.rows/2 == i)
{
//When in case number of rows are Odd size
//In This case reverse middle row element
col_size = this.cols/2;
}
else
{
col_size = this.cols;
}
for (int j = 0; j < col_size; ++j)
{
//Swap element
temp=matrix[i][j];
matrix[i][j]=matrix[row-i][col-j];
matrix[row-i][col-j]=temp;
}
}
}
//Display matrix
void show_data(int [][]matrix)
{
for (int i = 0; i < this.rows; ++i)
{
for (int j = 0; j < this.cols; ++j)
{
System.out.print(" "+matrix[i][j] );
}
System.out.print("\n");
}
System.out.print("\n");
}
public static void main(String[] args) {
int[][] matrix = {
{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}
};
MyMatrix obj = new MyMatrix(matrix);
obj.show_data(matrix);
obj.rotate_matrix(matrix);
obj.show_data(matrix);
}
}
Output
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
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
/*
C# Program
Rotate a Matrix by 180 degree
*/
using System;
public class MyMatrix {
int rows;
int cols;
MyMatrix(int[,] matrix) {
//Get matrix size
this.rows = matrix.GetLength(0);
this.cols = matrix.GetLength(1);
}
//rotate matrix elements in 180 degree
void rotate_matrix(int[,] matrix) {
int temp = 0;
int row = this.rows - 1, col = this.cols - 1;
int row_size = (this.rows / 2);
int col_size = this.cols;
if (this.rows % 2 == 1) {
//When in case number of rows are Odd size
row_size = (this.rows / 2) + 1;
}
for (int i = 0; i < row_size; ++i) {
if (this.rows / 2 == i) {
//When in case number of rows are Odd size
//In This case reverse middle row element
col_size = this.cols / 2;
} else {
col_size = this.cols;
}
for (int j = 0; j < col_size; ++j) {
//Swap element
temp = matrix[i,j];
matrix[i,j] = matrix[row - i,col - j];
matrix[row - i,col - j] = temp;
}
}
}
//Display matrix
void show_data(int[,] matrix) {
for (int i = 0; i < this.rows; ++i) {
for (int j = 0; j < this.cols; ++j) {
Console.Write(" " + matrix[i,j]);
}
Console.Write("\n");
}
Console.Write("\n");
}
public static void Main(String[] args) {
int[,] matrix = {
{
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
}
};
MyMatrix obj = new MyMatrix(matrix);
obj.show_data(matrix);
obj.rotate_matrix(matrix);
obj.show_data(matrix);
}
}
Output
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
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
<?php
/*
Php Program
Rotate a Matrix by 180 degree
*/
class MyMatrix {
public $rows;
public $cols;
function __construct($matrix) {
//Get matrix size
$this->rows = count($matrix);
$this->cols = count($matrix[0]);
}
//rotate matrix elements in 180 degree
function rotate_matrix(&$matrix) {
$temp = 0;
$row = $this->rows - 1;
$col = $this->cols - 1;
$row_size = (intval($this->rows / 2));
$col_size = $this->cols;
if ($this->rows % 2 == 1) {
//When in case number of rows are Odd size
$row_size = (intval($this->rows / 2)) + 1;
}
for ($i = 0; $i < $row_size; ++$i) {
if (intval($this->rows / 2) == $i) {
//When in case number of rows are Odd size
//In This case reverse middle row element
$col_size = intval($this->cols / 2);
} else {
$col_size = $this->cols;
}
for ($j = 0; $j < $col_size; ++$j) {
//Swap element
$temp = $matrix[$i][$j];
$matrix[$i][$j] = $matrix[$row - $i][$col - $j];
$matrix[$row - $i][$col - $j] = $temp;
}
}
}
//Display matrix
function show_data($matrix) {
for ($i = 0; $i < $this->rows; ++$i) {
for ($j = 0; $j < $this->cols; ++$j) {
echo(" ". $matrix[$i][$j]);
}
echo("\n");
}
echo("\n");
}
}
function main() {
$matrix = array(
array(1, 2, 3, 4, 5),
array(6, 7, 8, 9, 10),
array(11, 12, 13, 14, 15),
array(16, 17, 18, 19, 20),
array(21, 22, 23, 24, 25));
$obj = new MyMatrix($matrix);
$obj->show_data($matrix);
$obj->rotate_matrix($matrix);
$obj->show_data($matrix);
}
main();
Output
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
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
/*
Node Js Program
Rotate a Matrix by 180 degree
*/
class MyMatrix {
constructor(matrix) {
//Get matrix size
this.rows = matrix.length;
this.cols = matrix[0].length;
}
//rotate matrix elements in 180 degree
rotate_matrix(matrix) {
var temp = 0;
var row = this.rows - 1;
var col = this.cols - 1;
var row_size = (parseInt(this.rows / 2));
var col_size = this.cols;
if (this.rows % 2 == 1) {
//When in case number of rows are Odd size
row_size = (parseInt(this.rows / 2)) + 1;
}
for (var i = 0; i < row_size; ++i) {
if (parseInt(this.rows / 2) == i) {
//When in case number of rows are Odd size
//In This case reverse middle row element
col_size = parseInt(this.cols / 2);
} else {
col_size = this.cols;
}
for (var j = 0; j < col_size; ++j) {
//Swap element
temp = matrix[i][j];
matrix[i][j] = matrix[row - i][col - j];
matrix[row - i][col - j] = temp;
}
}
}
//Display matrix
show_data(matrix) {
for (var i = 0; i < this.rows; ++i) {
for (var j = 0; j < this.cols; ++j) {
process.stdout.write(" " + matrix[i][j]);
}
process.stdout.write("\n");
}
process.stdout.write("\n");
}
}
function main(args) {
var matrix = [
[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]
];
var obj = new MyMatrix(matrix);
obj.show_data(matrix);
obj.rotate_matrix(matrix);
obj.show_data(matrix);
}
main();
Output
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
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
# Python 3 Program
# Rotate a Matrix by 180 degree
class MyMatrix :
def __init__(self, matrix) :
# Get matrix size
self.rows = len(matrix)
self.cols = len(matrix[0])
def rotate_matrix(self, matrix) :
temp = 0
row = self.rows - 1
col = self.cols - 1
row_size = (int(self.rows / 2))
col_size = self.cols
if (self.rows % 2 == 1) :
# When in case number of rows are Odd size
row_size = (int(self.rows / 2)) + 1
i = 0
while (i < row_size) :
if (int(self.rows / 2) == i) :
# When in case number of rows are Odd size
# In This case reverse middle row element
col_size = int(self.cols / 2)
else :
col_size = self.cols
j = 0
while (j < col_size) :
# Swap element
temp = matrix[i][j]
matrix[i][j] = matrix[row - i][col - j]
matrix[row - i][col - j] = temp
j += 1
i += 1
def show_data(self, matrix) :
i = 0
while (i < self.rows) :
j = 0
while (j < self.cols) :
print(" ", matrix[i][j], end = "")
j += 1
print("\n", end = "")
i += 1
print("\n", end = "")
def main() :
matrix = [
[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]
]
obj = MyMatrix(matrix)
obj.show_data(matrix)
obj.rotate_matrix(matrix)
obj.show_data(matrix)
if __name__ == "__main__":
main()
Output
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
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
# Ruby Program
# Rotate a Matrix by 180 degree
class MyMatrix
# Define the accessor and reader of class MyMatrix
attr_reader :rows, :cols
attr_accessor :rows, :cols
def initialize(matrix)
# Get matrix size
self.rows = matrix.length
self.cols = matrix[0].length
end
def rotate_matrix(matrix)
temp = 0
row = self.rows - 1
col = self.cols - 1
row_size = (self.rows / 2)
col_size = self.cols
if (self.rows % 2 == 1)
# When in case number of rows are Odd size
row_size = (self.rows / 2) + 1
end
i = 0
while (i < row_size)
if (self.rows / 2 == i)
# When in case number of rows are Odd size
# In This case reverse middle row element
col_size = self.cols / 2
else
col_size = self.cols
end
j = 0
while (j < col_size)
# Swap element
temp = matrix[i][j]
matrix[i][j] = matrix[row - i][col - j]
matrix[row - i][col - j] = temp
j += 1
end
i += 1
end
end
def show_data(matrix)
i = 0
while (i < self.rows)
j = 0
while (j < self.cols)
print(" ", matrix[i][j])
j += 1
end
print("\n")
i += 1
end
print("\n")
end
end
def main()
matrix = [
[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]
]
obj = MyMatrix.new(matrix)
obj.show_data(matrix)
obj.rotate_matrix(matrix)
obj.show_data(matrix)
end
main()
Output
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
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
/*
Scala Program
Rotate a Matrix by 180 degree
*/
class MyMatrix(var rows: Int,var cols: Int) {
def this(matrix: Array[Array[Int]]) {
//Get matrix size
this(matrix.length,matrix(0).length);
}
def rotate_matrix(matrix: Array[Array[Int]]): Unit = {
var temp: Int = 0;
val row: Int = this.rows - 1;
val col: Int = this.cols - 1;
var row_size: Int = ((this.rows / 2).toInt);
var col_size: Int = this.cols;
if (this.rows % 2 == 1) {
//When in case number of rows are Odd size
row_size = ((this.rows / 2).toInt) + 1;
}
var i: Int = 0;
while (i < row_size) {
if ((this.rows / 2).toInt == i) {
//When in case number of rows are Odd size
//In This case reverse middle row element
col_size = (this.cols / 2).toInt;
} else {
col_size = this.cols;
}
var j: Int = 0;
while (j < col_size) {
//Swap element
temp = matrix(i)(j);
matrix(i)(j) = matrix(row - i)(col - j);
matrix(row - i)(col - j) = temp;
j += 1;
}
i += 1;
}
}
def show_data(matrix: Array[Array[Int]]): Unit = {
var i: Int = 0;
while (i < this.rows) {
var j: Int = 0;
while (j < this.cols) {
print(" " + matrix(i)(j));
j += 1;
}
print("\n");
i += 1;
}
print("\n");
}
}
object Main {
def main(args: Array[String]): Unit = {
var matrix: Array[Array[Int]] = Array(
Array(1, 2, 3, 4, 5),
Array(6, 7, 8, 9, 10),
Array(11, 12, 13, 14, 15),
Array(16, 17, 18, 19, 20),
Array(21, 22, 23, 24, 25));
val obj: MyMatrix = new MyMatrix(matrix);
obj.show_data(matrix);
obj.rotate_matrix(matrix);
obj.show_data(matrix);
}
}
Output
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
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
/*
Swift Program
Rotate a Matrix by 180 degree
*/
class MyMatrix {
var rows: Int;
var cols: Int;
init(_ matrix: [
[Int]
]) {
//Get matrix size
self.rows = matrix.count;
self.cols = matrix[0].count;
}
func rotate_matrix(_ matrix: inout [
[Int]
]) {
var temp: Int = 0;
let row: Int = self.rows - 1;
let col: Int = self.cols - 1;
var row_size: Int = (self.rows / 2);
var col_size: Int = self.cols;
if (self.rows % 2 == 1) {
//When in case number of rows are Odd size
row_size = (self.rows / 2) + 1;
}
var i: Int = 0;
while (i < row_size) {
if (self.rows / 2 == i) {
//When in case number of rows are Odd size
//In This case reverse middle row element
col_size = self.cols / 2;
} else {
col_size = self.cols;
}
var j: Int = 0;
while (j < col_size) {
//Swap element
temp = matrix[i][j];
matrix[i][j] = matrix[row - i][col - j];
matrix[row - i][col - j] = temp;
j += 1;
}
i += 1;
}
}
func show_data(_ matrix: [
[Int]
]) {
var i: Int = 0;
while (i < self.rows) {
var j: Int = 0;
while (j < self.cols) {
print(" ", matrix[i][j], terminator: "");
j += 1;
}
print("\n", terminator: "");
i += 1;
}
print("\n", terminator: "");
}
}
func main() {
var matrix: [
[Int]
] = [
[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]
];
let obj: MyMatrix = MyMatrix(matrix);
obj.show_data(matrix);
obj.rotate_matrix(&matrix);
obj.show_data(matrix);
}
main();
Output
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
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
Output Explanation
The mentioned C code implements the above algorithm to rotate a given matrix by 180 degrees. It swaps elements
symmetrically across the center vertical line. The original matrix is displayed using the show_data
function, then the matrix is rotated using the rotate_matrix
function, and the rotated matrix is
displayed again.
Time Complexity
The time complexity of the algorithm is O(R * C), where R is the number of rows and C is the number of columns in the matrix. This is because each element is swapped only once, and the algorithm iterates through each row and column once.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment