# Rotate a matrix by 90 degree without using any extra space in C++

C++Server Side ProgrammingProgramming

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 an anti-clockwise direction such that the first row becomes the first column, second row becomes second column and third 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 without using any extra space is:
4 12 9
1 16 8
5 9  2

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 12 9
1 16 8
5 9  2

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:
9 32 5
1 6  7
2 7  3

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-:
9 32 5
1 6  7
2 7  3

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

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

## 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
Published on 03-Nov-2021 07:23:46