
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Rotate a matrix by 90 degree in clockwise direction without using any extra space in C++
We are given a 2-D array that will be used to form a matrix pattern. The task is to rotate a matrix by 90 degrees in a clockwise direction such that the last row becomes the first column, second row becomes second column and first becomes third column and the challenge is that we don’t have to use any extra space.
Let us see various input output scenarios for this −
Input −
int arr[row_col_size][row_col_size] = { { 5, 1, 4}, { 9, 16, 12 }, { 2, 8, 9}}
Output −
Rotation of a matrix by 90 degree in clockwise direction without using any extra space is: 2 9 5 8 16 1 9 12 4
Explanation − we are given a 2-D array of integer type. Now we will rotate a matrix by 90 degrees in a clockwise direction.
Before rotation-: { { 5, 1, 4}, { 9, 16, 12 }, { 2, 8, 9}} After rotation-: 2 9 5 8 16 1 9 12 4
Input −
int arr[row_col_size][row_col_size] = { { 2, 1, 9}, { 11, 6, 32 }, { 3, 7, 5}}
Output −
Rotation of a matrix by 90 degree in clockwise direction without using any extra space is: 2 9 5 8 16 1 9 12 4
Explanation − we are given a 2-D array of integer type. Now we will rotate a matrix by 90 degrees in a clockwise direction.
Before rotation-: { { 2, 1, 9}, { 11, 6, 32 }, { 3, 7, 5}} After rotation-: 3 11 2 7 6 1 5 32 9
Approach used in the below program is as follows
1. Naive Approach
Input a 2-D integer array that will be treated as a matrix with row_col_size.
Pass the data to the function Rotate_ClockWise(arr).
Inside the function Rotate_ClockWise(arr)
Start loop FOR from i to 0 till i less than row_col_size/2.
Inside the loop, start another loop FOR from j to 0 till j less than row_col_size - i - 1.
Inside the loop, set ptr to arr[i][j], arr[i][j] to arr[row_col_size - 1 - j][i], arr[row_col_size - 1 - j][i] to arr[row_col_size - 1 - i][row_col_size - 1 - j], arr[row_col_size - 1 - i][row_col_size - 1 - j] to arr[j][row_col_size - 1 - i] and arr[j][row_col_size - 1 - i] to ptr.
Start loop FOR from i to 0 till i less than row_col_size. Inside the loop, start another loop FOR from j to 0 till j less than row_col_size; j++ and print arr[i][j].
2. Efficient Approach
Input a 2-D integer array that will be treated as a matrix with row_col_size.
Pass the data to the function Rotate_ClockWise(arr).
Inside the function Rotate_ClockWise(arr)
Start loop FOR from i to 0 till i less than row_col_size.
Inside the loop, start another loop FOR from j to 0 till j less than row_col_size - i.
Inside the loop, set ptr to arr[i][j], arr[i][j] to arr[row_col_size - 1 - j]arr[row_col_size - 1 - i] and [row_col_size - 1 - j] to arr[j][row_col_size - 1 - i] to ptr.
Start loop FOR from i to 0 till i less than row_col_size / 2. Inside the loop, start another loop FOR from j to 0 till j less than row_col_size. Inside the loop, set ptr to arr[i][j], arr[i][j] to arr[row_col_size - 1 - i][j] and arr[row_col_size - 1 - i][j] to ptr
Start loop FOR from i to 0 till i less than row_col_size. Inside the loop, start another loop FOR from j to 0 till j less than row_col_size; j++ and print arr[i][j].
Naive Approach
Example
#include <bits/stdc++.h> using namespace std; #define row_col_size 3 void Rotate_ClockWise(int arr[row_col_size][row_col_size]){ for(int i = 0; i < row_col_size / 2; i++){ for(int j = i; j < row_col_size - i - 1; j++){ int ptr = arr[i][j]; arr[i][j] = arr[row_col_size - 1 - j][i]; arr[row_col_size - 1 - j][i] = arr[row_col_size - 1 - i][row_col_size - 1 - j]; arr[row_col_size - 1 - i][row_col_size - 1 - j] = arr[j][row_col_size - 1 - i]; arr[j][row_col_size - 1 - i] = ptr; } } } int main(){ int arr[row_col_size][row_col_size] = { { 5, 1, 4},{ 9, 16, 12 },{ 2, 8, 9}}; Rotate_ClockWise(arr); cout<<"Rotation of a matrix by 90 degree in clockwise direction without using any extra space is: \n"; for(int i = 0; i < row_col_size; i++){ for(int j = 0; j < row_col_size; j++){ cout << arr[i][j] << " "; } cout << '\n'; } return 0; }
Output
If we run the above code it will generate the following Output
Rotation of a matrix by 90 degree in clockwise direction without using any extra space is: 2 9 5 8 16 1 9 12 4
Efficient Approach
Example
#include <bits/stdc++.h> using namespace std; #define row_col_size 3 void Rotate_ClockWise(int arr[row_col_size][row_col_size]){ for(int i = 0; i < row_col_size; i++){ for(int j = 0; j < row_col_size - i; j++){ int ptr = arr[i][j]; arr[i][j] = arr[row_col_size - 1 - j][row_col_size - 1 - i]; arr[row_col_size - 1 - j][row_col_size - 1 - i] = ptr; } } for(int i = 0; i < row_col_size / 2; i++){ for(int j = 0; j < row_col_size; j++){ int ptr = arr[i][j]; arr[i][j] = arr[row_col_size - 1 - i][j]; arr[row_col_size - 1 - i][j] = ptr; } } } int main(){ int arr[row_col_size][row_col_size] = { { 5, 1, 4},{ 9, 16, 12 },{ 2, 8, 9}}; Rotate_ClockWise(arr); cout<<"Rotation of a matrix by 90 degree in clockwise direction without using any extra space is: \n"; for(int i = 0; i < row_col_size; i++){ for(int j = 0; j < row_col_size; j++){ cout << arr[i][j] << " "; } cout << '\n'; } return 0; }
Output
If we run the above code it will generate the following Output
Rotation of a matrix by 90 degree in clockwise direction without using any extra space is: 2 9 5 8 16 1 9 12 4
- Related Articles
- Rotate a matrix by 90 degree without using any extra space in C++
- How to rotate a matrix of size n*n to 90 degree using C#?
- How to sort 0,1 in an Array without using any extra space using C#?
- Write a program in Java to rotate a matrix by 90 degrees in anticlockwise direction
- How to rotate a matrix of size n*n to 90-degree k times using C#?
- Rotate the matrix 180 degree in Java?
- Print n x n spiral matrix using O(1) extra space in C Program.
- Program to rotate square matrix by 90 degrees counterclockwise in Python
- How to sort 0,1,2 in an Array (Dutch National Flag) without extra space using C#?
- Print reverse of a Linked List without extra space and modification in C Program.
- Find pair for given sum in a sorted singly linked without extra space in C++
- C++ program to replace all occurrences of string AB with C without using extra space
- Find duplicate in an array in O(n) and by using O(1) extra space in C++
- Print a given matrix in counter-clockwise spiral form in C++
- Print a pattern without using any loop in C++
