You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). You need to do this in place. Note that if you end up using an additional array, you will only receive partial score. - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Tuesday, 2 June 2020

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). You need to do this in place. Note that if you end up using an additional array, you will only receive partial score.


You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).
You need to do this in place. Note that if you end up using an additional array, you will only receive a partial score.

Here is a simple and easy approach with using transpose to reverse the matrices. Just print from the bottom to the top . Give execution time is 0.1s.


#include <iostream>
using namespace std;

int main() {
//code
int t;
cin>>t;
while(t--)
{
    int n;
    cin>>n;
    int a[n][n];
    for(int i=0;i<n;++i)
    {
        for(int j=0;j<n;++j)
        {
            cin>>a[i][j];
        }
    }
    
    for(int i=0;i<n;++i)
    {
        
        for(int j=n-1;j>=0;--j)
        {
            cout<<a[j][i]<< " ";
        }
    }
    cout<<endl;
    
    
}
return 0;
}