Array Rotation 1
InterviewTime limit1sMemory limit512 MB
Rotate each concentric ring of an N by M matrix counterclockwise R times, then print the resulting matrix.
- Level
Medium5 of 10
- Topics
- Matrix, Simulation, Implementation, Array
- Solved
- No attempts yet
Problem
Given an array of size N×M, we want to rotate it. The array must be rotated counterclockwise as shown below.
A[1][1] ← A[1][2] ← A[1][3] ← A[1][4] ← A[1][5]
↓ ↑
A[2][1] A[2][2] ← A[2][3] ← A[2][4] A[2][5]
↓ ↓ ↑ ↑
A[3][1] A[3][2] → A[3][3] → A[3][4] A[3][5]
↓ ↑
A[4][1] → A[4][2] → A[4][3] → A[4][4] → A[4][5]
Given an array and an integer R, find the result of rotating the array R times.
Input
The first line gives the array dimensions N and M and the number of rotations R to perform.
The next N lines give the elements Aij of array A.
Output
Print the result of rotating the given array R times.
Constraints
- 2 ≤ N, M ≤ 300
- 1 ≤ R ≤ 1,000
- min(N, M) mod 2 = 0
- 1 ≤ Aij ≤ 10^8