Extra array
Time O(n) Space O(n)Element i moves to (i + k) % n. Write each one into a new array, then copy back.
class Solution {
public void rotate(int[] nums, int k) {
int n = nums.length;
int[] out = new int[n];
for (int i = 0; i < n; i++) out[(i + k) % n] = nums[i];
System.arraycopy(out, 0, nums, 0, n);
}
}Verdict: Simple, but the follow-up asks for O(1) space.