Implementation in C++
// c++ program for rotating an array
#include<bits/stdc++.h>
using namespace std;
// function to print the matrix
// n and m represents number of rows and number of columns in the matrix respectively
void printMatrix(vector<vector<int>>&arr,int n, int m){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cout << arr[i][j] << " ";
}
cout << endl;
}
}
// function to rotating an array in clockwise direction by 90 degrees
void rotateMatrix(vector<vector<int>>&input, vector<vector<int>>&output, int n, int m){
// p will be used to traverse the column in the output matrix
int p = n1;
// traversing the input array in row wise fashion,
// and storing the values in the output matrix
for(int i=0;i<n;i++){
// used to traverse the row in the output matrix
int k = 0;
for(int j=0;j<m;j++){
int value = input[i][j];
output[k++][p] = value;
}
p;
}
return;
}
int main(){
int n = 4, m = 3;
// declaring the input array
vector<vector<int>>input
{ {1,2,3},
{5,6,7},
{1,2,3},
{5,6,7}
};
// declaring the output array to store the rotated image
// number of rows and columns will be interchanged in rotated array
// as compare to the input array
vector<vector<int>>output(m, vector<int>(n, 0));
cout << "Matrix before rotation: " << endl;
// printing the matrix before rotation
printMatrix(input, n, m);
// calling the function to store the rotated image in the
// output matrix
rotateMatrix(input, output, n, m);
cout << "Matrix after rotation 90 degrees: " << endl;
// printing the matrix after rotation
printMatrix(output, m, n);
}
Output:
Matrix before rotation:
1 2 3
5 6 7
1 2 3
5 6 7
Matrix after rotation 90 degrees:
5 1 5 1
6 2 6 2
7 3 7 3
Try and compile by yourself with the help of online C++ Compiler for better understanding.
Complexity Analysis
Time Complexity: O(n*m), Since we are traversing the input matrix only once, the time complexity is O(n*m), where n is the number of rows and m is the number of columns.
Space Complexity: O(n*m), We are just creating an auxiliary array to store the rotated images, so total space complexity is O(n*m), where n is the number of rows, and m is the number of columns.
Also read  Decimal to Binary c++
FAQs

What is the main difference between static and dynamic memory allocation?
Static memory allocation allocates variables indefinitely until the code executes or the function call completes. Variables are allocated in Dynamic memory allocation only if your program unit is active.

What is a matrix?
A matrix is a rectangular array of integers organized into rows and columns. The numbers are referred to as the matrix's elements or entries. Engineering, physics, economics, statistics, and many disciplines of mathematics all use matrices.

What is the greedy approach?
It is an algorithm to get the optimal solution for the problem. In this algorithm, we always choose the next best solution that seems optimal at that step. We build solutions piece by piece to reach the optimal solution.
Key Takeaways
In this article, we discussed the problem of rotating an array in a clockwise direction by 90 degrees; we have first understood the problem statement and then discussed some sample cases and then its solution approach along with the code in c++. We hope you understand the problem and solution properly. Now you can do more similar questions.
Check out this problem  Matrix Median
If you are a beginner, interested in Coding, and want to learn DSA, you can look for our guided path for DSA, which is free!
Thank you for reading.
Until then, Keep Learning and Keep Coding