Skip to content

Instantly share code, notes, and snippets.

@AliAkberAakash
Last active January 6, 2020 18:48
Show Gist options
  • Save AliAkberAakash/ea336326a246bd6c7ce813267545626b to your computer and use it in GitHub Desktop.
Save AliAkberAakash/ea336326a246bd6c7ce813267545626b to your computer and use it in GitHub Desktop.
/*
============================================================================
* Author : Ali Akber Aakash
* Email : ali852609@gmail.com
* Problem Name : Matrix Rotation
* Source : Cracking The Coding Interview - Gayle Laakmann McDowell
* Time Limit : 1.00 sec
============================================================================
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
//setting up the matrix
int matrix[4][4]={
1,2,3,4,
5,6,7,8,
9,10,11,12,
13,14,15,16
};
//length of the matrix
int n = 4;
//traversing through the layers
for(int layer=0; layer<n/2; layer++)
{
int first = layer;
int last = n-layer-1;
//traverse through start to end
for(int i=first; i<last; i++)
{
//creating the offset
int offset = i - first;
//storing the top value
int top = matrix[first][i];
//store left in top
matrix[first][i] = matrix[last-offset][first];
//store bottom in left
matrix[last-offset][first] = matrix[last][last-offset];
//store right in bottom
matrix[last][last-offset] = matrix[i][last];
//store top in right
matrix[i][last] = top;
}
}
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
cout<<matrix[i][j]<<" ";
cout<<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment