# How would you rotate an NxN matrix by 90 degrees clockwise?

988 views

.

posted Nov 18, 2013

``````void rotate(int[][] matrix, int n)
{
for (int layer = 0; layer < n / 2; ++layer)
{
int first = layer;
int last = n - 1 - layer;
for(int i = first; i < last; ++i)
{
int offset = i - first;
int top = matrix[first][i];

matrix[first][i] = matrix[last-offset][first];

matrix[last-offset][first] = matrix[last][last - offset];

matrix[last][last - offset] = matrix[i][last];

matrix[i][last] = top;
}
}
}
``````

Take transpose of matrix and then filp it:-
Complete c++ program

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

void swap(int* a,int *b)
{
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
return;
}

int main()
{
int n;
int i,j;
cin>>n;
int matrix[n][n];

//input the matrix
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>matrix[i][j];
}
}

//print the original matrix
cout<<"The original matrix\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}

//Transpose
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
swap(&matrix[i][j],&matrix[j][i]);
}
}
//Flip
for(i=0;i<n;i++)
{
for(j=0;j<n/2;j++)
{
swap(&matrix[i][j],&matrix[i][n-j-1]);
}
}
cout<<"The matrix after 90 degree clockwise rotation\n";
//print the result
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}

return 0;
}
``````

Tested and working C++ code, You can test here : My Code School

``````#include<iostream>
#include<cstdio>
#include<cmath>

using namespace std;
int main()
{
int t, n, tmp, jhn, khn;
cin >> t;
for(int i=0; i<t; i++)
{
cin >> n;
int ** a = new int*[n];
for(int j=0; j<n; j++)
{
a[j] = new int[n];
}
for(int j=0; j<n; j++)
{
for(int k=0; k<n; k++)
{
cin >> a[j][k];
}
}
if(n%2 == 0)
{
jhn = n/2;
khn = n/2;
}
else
{
jhn = (n+1)/2;
khn = (n-1)/2;
}
for(int j=0; j<jhn; j++)
{
for(int k=0; k<khn; k++)
{
tmp = a[j][k];
a[j][k] = a[n-k-1][j];
a[n-k-1][j] = a[n-j-1][n-k-1];
a[n-j-1][n-k-1] = a[k][n-j-1];
a[k][n-j-1] = tmp;
}
}
for(int j=0; j<n; j++)
{
for(int k=0; k<n; k++)
{
cout << a[j][k];
if(k != n-1) cout << " ";
}
cout << endl;
}
for(int j=0; j<n; j++)
{
delete[] a[j];
}
delete[] a;
cout << endl;
}
}
``````
for(int i=0; i<t; i++)
{
for(int j=0; j<n; j++)
{
for(int k=0; k<n; k++)
}
for(int j=0; j<jhn; j++)
{
for(int k=0; k<khn; k++)
{
}
}
for(int j=0; j<n; j++)
{
for(int k=0; k<n; k++)
{
}
cout << endl;
}
for(int j=0; j<n; j++)
{
}

}

What you did with the complexity.
Similar Questions

Write a c program that rotate elements of an array by the value of configured number "n".
For example:
Input array[ ] = { 2, 3, 5, 6, 8, 9}
value of n = 2
Output array[] = { 5, 6, 8, 9, 2, 3}
I am looking for efficient program.

Given a matrix with 1s and 0s, please find the number of groups of 1s. A group is defined by horizontally or vertically adjacent 1s.

How to implement a function to check whether there is a path for a string in a matrix of characters? It moves to left, right, up and down in a matrix, and a cell for a movement. The path can start from any entry in a matrix. If a cell is occupied by a character of a string on the path, it cannot be occupied by another character again.

You have a 2D matrix. Only two ZEROs in matrix.
Find the path from 1st zero to 2nd zero with least sum.

``````1       6       8       9       0       3

4       9       -5      5       11      13

8       9       44      23      15      -20

7       9       7       -13     14      11

0       16      23      31      16      7

67      5       4       23      21      19
``````

``````1       6       8       9       0  ----> 3
|
4       9       -5      5       11      13
|
8       9       44      23      15      -20
|
7 <---- 9 <---- 7 <--- -13 <--- 14 <---  11
|
0       16      23      31      16        7

67      5       4       23      21       19
``````