Shift every element of the integer array nums k places to the right. An element pushed off the right end comes back in at the left end, so after one shift the last element is first.
Change nums itself and return nothing. The judge reads nums after your function finishes, and that is the output shown in each example.
k can be larger than the array's length.
The last two elements, 50 and 60, wrap round to the front, and everything else moves two places right.
Three shifts bring a length-3 array back to where it started, so four shifts land in the same place as one.
Constraints
1 <= nums.length <= 10⁵-2³¹ <= nums[i] <= 2³¹ - 10 <= k <= 10⁵Rotating n places changes nothing, so only k % n matters. After that, the last k elements end up in front, in the order they were already in.
First, shrink k. After n shifts every value is back where it started, so shifting k places is the same as shifting k % n places. From here on, k means k % n, which is less than n.
Time O(n)Space O(1)
The full walkthrough and code are part of Premium