Some notes that can be used to speed things up:
- Rotating by K, when K>N, is equivalent to rotating by K%N (as each rotation of N is an expensive no-op).
- You don’t actually need to generate the rotated array; you just need to print the values as they would appear in the rotated array.
- In fact, you don’t (necessarily) need to store all of the input in an array, but just the part that can’t be printed until the last value in the array is read. (This would get the input and output intermingled if both were through the console, but not if at least one were a file.)
These can be used to turn your O(N^2) solution into an O(N) one.
1
solved Rotation of integers stored in an array