C++ Program to Rotate a Matrix by 180 degree
Last Updated :
17 Aug, 2023
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++
// C++ program to rotate a matrix by 180 degrees
#include <bits/stdc++.h>
#define N 3
using namespace std;
// Function to Rotate the matrix by 180 degree
void rotateMatrix(int mat[][N])
{
// Simply print from last cell to first cell.
for (int i = N - 1; i >= 0; i--) {
for (int j = N - 1; j >= 0; j--)
printf("%d ", mat[i][j]);
printf("
");
}
}
// Driven code
int main()
{
int mat[N][N] = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
rotateMatrix(mat);
return 0;
}
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++
// C++ program for left rotation of matrix by 180
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
// Function to rotate the matrix by 180 degree
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]);
}
// Function for transpose of matrix
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]);
}
// Function for display the matrix
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 << '
';
}
}
// Function to anticlockwise rotate matrix
// by 180 degree
void rotate180(int arr[R][C])
{
transpose(arr);
reverseColumns(arr);
transpose(arr);
reverseColumns(arr);
}
// Driven code
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;
}
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;
/**
* Reverse Row at specified index in the matrix
* @param data matrix
* @param index row index
*/
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;
}
}
/**
* Print Matrix data
* @param data matrix
*/
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;
}
}
/**
* Rotate Matrix by 180 degrees
* @param data matrix
*/
void rotateMatrix180(vector<vector<int>>& data)
{
int rows = data.size();
int cols = data[0].size();
if (rows % 2 != 0)
{
// If N is odd reverse the middle
// row in the matrix
reverseRow(data, data.size() / 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;
}
}
}
// Driver code
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 } };
// Rotate Matrix
rotateMatrix180(data);
// Print Matrix
printMatrix(data);
return 0;
}
// This code is contributed by divyeshrabadiya07
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++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is
15 min read
Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio
10 min read
Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i
7 min read
Templates in C++ C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so
9 min read
C++ Interview Questions and Answers (2025) C++ - the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent i
15+ min read
Operator Overloading in C++ in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot
8 min read
C++ Standard Template Library (STL) The C++ Standard Template Library (STL) is a set of template classes and functions that provides the implementation of common data structures and algorithms such as lists, stacks, arrays, sorting, searching, etc. It also provides the iterators and functors which makes it easier to work with algorith
9 min read
Map in C++ STL In C++, maps are associative containers that store data in the form of key value pairs sorted on the basis of keys. No two mapped values can have the same keys. By default, it stores data in ascending order of the keys, but this can be changes as per requirement.Example:C++#include <bits/stdc++.h
8 min read