Table of contents
1.
Introduction
1.1.
Sample Examples
2.
Solution Approach 
2.1.
Steps of algorithm
3.
Implementation in C++
3.1.
Complexity Analysis
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Turn an image by 90 degrees

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

This blog will discuss the problem of rotating an array by 90 degrees. The problem states that we are given characters in the 2D matrix of size MxN, and we need to rotate that matrix by 90 degrees clockwise, i.e., 90 degrees to the right.  

Also See, Image Sampling 

Sample Examples

Example 1:

Input:
# # # # ^ # #  
# # # # | # # 
# # # # | # # 

Output: 
# # # 
# # # 
# # # 
# # # 
- - >
# # #
# # #
 
Explanation: If we look at the direction of the arrow, in input and output, then we can say that output image is rotated by 90 degrees in the right.  

 

Example 2:

Input:
1 2 3 4 
5 6 7 8 
1 2 3 4 
5 6 7 8 

Output: 
5 1 5 1 
6 2 6 2 
7 3 7 3 
8 4 8 4 

Solution Approach 

For rotating an array, we will use an extra buffer to store our final output. If we carefully look at the input and output, we can see that:

  1. The first row of input is the last column of the output. 
  2. Similarly, the second row of input is the last second column of the output, and so on. 
  3. The last row of input is the first column of the output. 

 

So we will make an extra buffer to store the rotated image of the input matrix. If the size of the input matrix is [n][m], then the size of the output matrix will be [m][n], as a number of rows and number of columns will be interchanged. 

Steps of algorithm

  1. Create the output matrix of size output[number of columns in input][number of rows in input] for rotating an array. 
  2. Traverse the input matrix row-wise, traverse the output matrix from the last column, and simultaneously copy the data from the input matrix to the output matrix. 
  3. Finally, print the output matrix. 

Also read, Application of Oops

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 = n-1;

    // 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);
}
You can also try this code with Online C++ Compiler
Run Code


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

  1. 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.
     
  2. 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. 
     
  3. 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

Live masterclass