Rotate a matrix clockwise by 90 degree without using any extra space | Set 3 - GeeksforGeeks

09 Jul, 2021
Given a rectangular matrix mat[][] with N rows and M columns, the task is to rotate the matrix by 90 degrees in a clockwise direction without using extra space.
Examples:
Input: mat[][] = {{1, 2, 3},  {4, 5, 6},   {7, 8, 9},  {10, 11, 12}}Output: 10 7 4 1                11 8 5 2                12 9 6 3
Input: mat[][] = {{1, 2, 3, 4, 5, 6}, {7, 8, 9, 10, 11, 12}}Output: 7 1               8 2               9 3              10 4              11 5              12 6
Note: The approach to rotate square matrix is already discussed as follows:  
With extra space: Inplace rotate square matrix by 90 degrees | Set 1
Without extra space in anti-clockwise direction: Rotate a matrix by 90 degree without using any extra space | Set 2
Approach: The main idea is to perform an in-place rotation.
Follow the below steps to solve the given problem:
Swap all the elements of the sub-matrix min(N, M) * min(N, M), along the main diagonal i.e from the upper top corner to the bottom right corner.If N is greater than M,Push all the unswapped elements of each column i where (min(N, M) ≤ i) to the ith row.Otherwise, push all the unswapped elements of each row i where (min(N, M) ≤ i) to the ith column.Reverse each row of the matrixPrint the updated matrix of dimension M × N.
Swap all the elements of the sub-matrix min(N, M) * min(N, M), along the main diagonal i.e from the upper top corner to the bottom right corner.
If N is greater than M,Push all the unswapped elements of each column i where (min(N, M) ≤ i) to the ith row.Otherwise, push all the unswapped elements of each row i where (min(N, M) ≤ i) to the ith column.
Push all the unswapped elements of each column i where (min(N, M) ≤ i) to the ith row.
Otherwise, push all the unswapped elements of each row i where (min(N, M) ≤ i) to the ith column.
Reverse each row of the matrix
Print the updated matrix of dimension M × N.
Procedure:
Let the given matrix be : 1 2 3 4 5 6 7 8 9 10 11 1213 14 15Swap all the elements of the sub-matrix min(N, M) * min(N, M) i.e 3 * 3 for this example1 4 72 5 83 6 910 11 1213 14 15
Since N > M, push all the unswapped elements of each column i (min(N, M) ≤ i) to the ith row1 4 7 10 132 5 8 11 143 6 9 12 15
Reverse each column to get the final rotated matrix as:13 10 7 4 114 11 8 5 215 12 9 6 3
Below is the implementation of the above approach:
C++
// C++ program for// the above approach#include <bits/stdc++.h>using namespace std;  // Function to print the matrix mat// with N rows and M columnsvoid print(vector<vector<int> > mat,           int N, int M){    for (int i = 0; i < N; i++) {        for (int j = 0; j < M; j++) {            cout << mat[i][j] << " ";        }          cout << "\n";    }}  // Function to rotate the matrix// by 90 degree clockwisevoid rotate(vector<vector<int> > mat){    // Number of rows    int N = mat.size();      // Number of columns    int M = mat[0].size();      // Swap all the elements along main diagonal    // in the submatrix min(N, M) * min(N, M)    for (int i = 0; i < min(N, M); i++) {        for (int j = i; j < min(N, M); j++) {              // Swap mat[i][j] and mat[j][i]            swap(mat[i][j], mat[j][i]);        }    }      // If N is greater than M    if (N > M) {          // Push all the unswapped elements        // of ith column to ith row        for (int i = 0; i < M; i++) {            for (int j = min(N, M); j < N; j++) {                mat[i].push_back(mat[j][i]);            }        }    }    else {        // Resize mat to have M rows        mat.resize(M, {});          // Push all the unswapped elements        // of ith column to ith row        for (int i = min(N, M); i < M; i++) {            for (int j = 0; j < N; j++) {                mat[i].push_back(mat[j][i]);            }        }    }      // Reverse each row of the matrix    for (int i = 0; i < M; i++) {        reverse(mat[i].begin(), mat[i].end());    }      // Print the final matrix    print(mat, M, N);}  // Driver Codeint main(){    // Input    vector<vector<int> > mat = { { 1, 2, 3 },                                 { 4, 5, 6 },                                 { 7, 8, 9 },                                 { 10, 11, 12 } };      // Function Call    rotate(mat);      return 0;}
10 7 4 1 
11 8 5 2 
12 9 6 3 
Time Complexity: O(N * M)Auxiliary Space: O(1)
 
adarsh08x
goyalhimanshu
diehard93
cpp-vector
rotation
Arrays
C++ Programs
Matrix
Arrays
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count pairs with given sum
Chocolate Distribution Problem
Window Sliding Technique
Reversal algorithm for array rotation
Next Greater Element
Header files in C/C++ and its uses
Program to print ASCII Value of a character
How to return multiple values from a function in C or C++?
C++ Program for QuickSort
Sorting a Map by value in C++ STL