You are given an n x n 2D matrix rotate it by 90 degrees (clockwise) in C++.

Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Example 1:

Given input matrix = 
[
  [1,2,3],
  [4,5,6],
  [7,8,9]
],

rotate the input matrix in-place such that it becomes:
[
  [7,4,1],
  [8,5,2],
  [9,6,3]
]

We can solve this by 2 different ways.

1. By reversing and swapping
2. By flipping

1. By Rotating and swapping the elements.

In this solution we first reverse the array, means 1st row will go to 3rd row and 3rd row will come to the first row.

Hence if our input is:
1 2 3
4 5 6
7 8 9

after reversing the array, the output will be:
7 8 9
4 5 6
1 2 3

Then you swap the elements diagonally, after this step, the output will be:
7 4 1
8 5 2
9 6 3

Hence our final result.

2. By flipping

In this solution, we flip the array 2 times. One is diagonal flip and Second is horizontal flip.

If our original array is:
1 2 3
4 5 6
7 8 9

After diagonal flip, the output will be:

1 4 7
2 5 8
3 6 9

After horizontal flip, the output will be:
7 4 1
8 5 2
9 6 3

The final array.

Solution in C++

#include<iostream>
#include<vector>
using namespace std;

void rotate(vector<vector<int> >& myMatrix) 
{
       
    reverse(myMatrix.begin(), myMatrix.end());

	int size = myMatrix.size();

	for (int i = 0; i < size; ++i)
    {
		for (int j = i+1; j < size; ++j)
		{
			swap(myMatrix[i][j], myMatrix[j][i]);
		}
	}
}


void flip(vector<vector<int> >& myMatrix) 
{
	int size = myMatrix.size();

  	/*Flip Diagonally*/
	for (size_t i = 0; i < size; i++) 
	{
		for (size_t j = i; j < size; j++) 
		{
			swap(myMatrix[i][j], myMatrix[j][i]);
		}
    }
        /* flip horizontally  */
	for (size_t i = 0; i < size; i++) 
	{
		reverse(myMatrix[i].begin(), myMatrix[i].end());
    }
}

int main() 
{
	//Declare a 2d array
	vector<vector<int> > myMatrix;
	int row = 3;
	int column = 3;

	int temp = 1; // variable to put the values in to vector

	//populate the array
	for(int i=0; i < row; i++)
	{
		//create a temp vector, it will act as a row
		vector<int>  temp_vector;
 
		for(int i=0; i<column; i++)
		{
			temp_vector.push_back(temp);
			temp++;
		}
		// push the content of temp vector to the main vector
		myMatrix.push_back(temp_vector); 
	}

cout<< "Entered vector is : "<<endl;
	for (int i = 0; i < row; i++) 
	{
            for (int j = 0; j < column; j++)
                cout << myMatrix[i][j] << ' ';
            cout << endl;
    }

//rotate(myMatrix);
flip(myMatrix);

    cout<< "Final vector is : "<<endl;
	for (int i = 0; i < row; i++) 
	{
            for (int j = 0; j < column; j++)
                cout << myMatrix[i][j] << ' ';
            cout << endl;
    }

    return 0;
}

Output:

Entered vector is :
1 2 3
4 5 6
7 8 9
Final vector is :
7 4 1
8 5 2
9 6 3
Share your solution below in comment section.

 

 

 

 

 

 

 

 

 

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *