I was solving problems at HackerRank when I was stuck with this.
Problem Statement
You are given a two-dimensional matrix a, dimension MxN and a positive integer R. You need to rotate the matrix R times and print the resulting matrix. The rotation should be counterclockwise.
The rotation of the 4x5 matrix is โโshown in the following figure. Please note that in one revolution you should only move elements one step (for clarity, see Test Examples). 
It is guaranteed that the minimum of M and N will be even.
entrance
The first line contains three integers, separated by a space, M, N and R, where M is the number of rows, N is the number of columns in the matrix, and R is the number of times to be rotated. This is followed by the line M, where each line contains N spaces, separated by positive integers. These M-rows are a matrix.
Output
Print the rotated matrix.
Limitations
2 <= M, N <= 300 1 <= R <= 10^9 min(M, N) % 2 == 0 1 <= aij <= 108, where i โ [1..M] & j โ [1..N]'
What I was trying to do was save circles in a 1D array. Something like that.
while(true) { k = 0; for(int j = left; j <= right; ++j) {temp[k] = a[top][j]; ++k;} top++; if(top > down || left > right) break; for(int i = top; i <= down; ++i) {temp[k] = a[i][right]; ++k;} right--; if(top > down || left > right) break; for(int j = right; j >= left; --j) {temp[k] = a[down][j] ; ++k;} down--; if(top > down || left > right) break; for(int i = down; i >= top; --i) {temp[k] = a[i][left]; ++k;} left++; if(top > down || left > right) break; }
Then I could easily rotate the 1D matrix by calculating its length modulo R. But then how can I return it in matrix form? Using a loop can again result in a timeout.
Please do not provide a code, but give only suggestions. I want to do it myself.
Created solution:
#include <iostream> using namespace std; int main() { int m,n,r; cin>>m>>n>>r; int a[300][300]; for(int i = 0 ; i < m ; ++i){ for(int j = 0; j < n ; ++j) cin>>a[i][j]; } int left = 0; int right = n-1; int top = 0; int down = m-1; int tleft = 0; int tright = n-1; int ttop = 0; int tdown = m-1; int b[300][300]; int k,size; int temp[1200]; while(true){ k=0; for(int i = left; i <= right ; ++i) { temp[k] = a[top][i]; // cout<<temp[k]<<" "; ++k; } ++top; if(top > down || left > right) break; for(int i = top; i <= down ; ++i) { temp[k]=a[i][right]; // cout<<temp[k]<<" "; ++k; } --right; if(top > down || left > right) break; for(int i = right; i >= left ; --i) { temp[k] = a[down][i]; // cout<<temp[k]<<" "; ++k; } --down; if(top > down || left > right) break; for(int i = down; i >= top ; --i) { temp[k] = a[i][left]; // cout<<temp[k]<<" "; ++k; } ++left; if(top > down || left > right) break; //________________________________\\ size = k; k=0; // cout<<size<<endl; for(int i = tleft; i <= tright ; ++i) { b[ttop][i] = temp[(k + (r%size))%size]; // cout<<(k + (r%size))%size<<" "; // int index = (k + (r%size))%size; // cout<<index; ++k; } ++ttop; for(int i = ttop; i <= tdown ; ++i) { b[i][tright]=temp[(k + (r%size))%size]; ++k; } --tright; for(int i = tright; i >= tleft ; --i) { b[tdown][i] = temp[(k + (r%size))%size]; ++k; } --tdown; for(int i = tdown; i >= ttop ; --i) { b[i][tleft] = temp[(k + (r%size))%size]; ++k; } ++tleft; } size=k; k=0; if(top != ttop){ for(int i = tleft; i <= tright ; ++i) { b[ttop][i] = temp[(k + (r%size))%size]; ++k; } ++ttop; } if(right!=tright){ for(int i = ttop; i <= tdown ; ++i) { b[i][tright]=temp[(k + (r%size))%size]; ++k; } --tright; } if(down!=tdown){ for(int i = tright; i >= tleft ; --i) { b[tdown][i] = temp[(k + (r%size))%size]; ++k; } --tdown; } if(left!=tleft){ for(int i = tdown; i >= ttop ; --i) { b[i][tleft] = temp[(k + (r%size))%size]; ++k; } ++tleft; } for(int i = 0 ; i < m ;++i){ for(int j = 0 ; j < n ;++j) cout<<b[i][j]<<" "; cout<<endl; } return 0; }