You are given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
Example 1:
Input: nums = [1,2,3,4,5,6,7,8], k = 4
Output: [5,6,7,8,1,2,3,4]Explanation:
rotate 1 steps to the right: [8,1,2,3,4,5,6,7]
rotate 2 steps to the right: [7,8,1,2,3,4,5,6]
rotate 3 steps to the right: [6,7,8,1,2,3,4,5]
rotate 4 steps to the right: [5,6,7,8,1,2,3,4]
Example 2:
Input: nums = [1000,2,4,-3], k = 2
Output: [4,-3,1000,2]Explanation:
rotate 1 steps to the right: [-3,1000,2,4]
rotate 2 steps to the right: [4,-3,1000,2]
Constraints:
1 <= nums.length <= 100,000-(2^31) <= nums[i] <= ((2^31)-1)0 <= k <= 100,000Follow up: Could you do it in-place with O(1) extra space?
Before attempting this problem, you should be comfortable with:
The simplest way to rotate an array by k positions is to perform k single rotations. In each rotation, we save the last element, shift every element one position to the right, and place the saved element at the front. This mimics the physical act of rotating items in a line. While straightforward, this approach is slow because we repeat the entire shift process k times.
k = k % n to handle cases where k is larger than the array length.k times:0.Instead of repeatedly shifting elements, we can directly compute the final position of each element. If an element is at index i, after rotation it will be at index (i + k) % n. By using a temporary array to store the rotated result, we can place each element in its correct position in a single pass, then copy everything back.
tmp of the same size as nums.i, place nums[i] at position (i + k) % n in tmp.tmp back into nums.We can rotate in-place by following cycles. Starting from any position, we move the element to its destination, then move the displaced element to its destination, and so on until we return to the starting position. If the cycle doesn't cover all elements (which happens when n and k share a common divisor), we start a new cycle from the next position. This ensures every element is moved exactly once.
k = k % n and initialize a counter for how many elements have been placed.0. For each starting index:(current + k) % n, swap the saved element with the element there, and repeat.n elements have been moved.class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
n = len(nums)
k %= n
count = start = 0
while count < n:
current = start
prev = nums[start]
while True:
next_idx = (current + k) % n
nums[next_idx], prev = prev, nums[next_idx]
current = next_idx
count += 1
if start == current:
break
start += 1A clever observation: rotating an array by k is equivalent to moving the last k elements to the front. We can achieve this with three reversals. First, reverse the entire array. Now the last k elements are at the front, but in reverse order. Reverse the first k elements to fix their order. Finally, reverse the remaining elements to restore their original order.
k = k % n.0 to n - 1.k elements (from index 0 to k - 1).k to n - 1).class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
n = len(nums)
k %= n
def reverse(l: int, r: int) -> None:
while l < r:
nums[l], nums[r] = nums[r], nums[l]
l, r = l + 1, r - 1
reverse(0, n - 1)
reverse(0, k - 1)
reverse(k, n - 1)Many languages provide built-in ways to slice and concatenate arrays. We can simply take the last k elements and prepend them to the rest of the array. This leverages language features to express the rotation concisely, though under the hood it may use extra space.
k % n.k elements and the first n - k elements.When k is larger than the array length n, rotating by k is the same as rotating by k % n. Failing to normalize k with the modulo operation leads to unnecessary iterations or index out of bounds errors. Always compute k = k % n before proceeding.
In the reverse approach, the three reversals must use precise boundaries: reverse the entire array, then reverse [0, k-1], then reverse [k, n-1]. Using wrong indices like reversing [0, k] instead of [0, k-1] shifts the boundary incorrectly and produces a wrong result.
In the cyclic replacement approach, failing to track how many elements have been placed can cause infinite loops or incomplete rotations. This happens when n and k share a common divisor, creating multiple cycles. Always maintain a counter and start new cycles until all elements are moved.