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], k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4]
Example 2:
Input: nums = [-1,-100,3,99], k = 2 Output: [3,99,-1,-100] Explanation: rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100]
Constraints:
1 <= nums.length <= 105
-231 <= nums[i] <= 231 - 1
0 <= k <= 105
Follow up:
O(1)
extra space?When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method is like physically moving items in a line. We shift each item one by one, repeatedly, until they've moved the required amount. It's simple but not the fastest way.
Here's how the algorithm would work step-by-step:
def rotate_array_brute_force(numbers, rotation_amount):
array_length = len(numbers)
for _ in range(rotation_amount):
# Store the last element before rotation.
last_element = numbers[array_length - 1]
# Shift elements to the right.
for index in range(array_length - 1, 0, -1):
numbers[index] = numbers[index - 1]
# Place the last element at the beginning.
numbers[0] = last_element
return numbers
To rotate a collection of items efficiently, we want to avoid moving items one at a time multiple times. The best way to do this is to think about reversing parts of the collection.
Here's how the algorithm would work step-by-step:
def rotate_array(nums, rotation_steps):
array_length = len(nums)
rotation_steps %= array_length
def reverse_part_of_array(nums, start, end):
while start < end:
nums[start], nums[end] = nums[end], nums[start]
start += 1
end -= 1
# Reversing the entire array is the first step
reverse_part_of_array(nums, 0, array_length - 1)
# Reversing the first part of the array
reverse_part_of_array(nums, 0, rotation_steps - 1)
# Reversing the second part of the array
reverse_part_of_array(nums, rotation_steps, array_length - 1)
return nums
Case | How to Handle |
---|---|
Null or empty array | Check for null or empty input and return immediately to avoid errors; nothing to rotate. |
k is zero | If k is zero, no rotation is needed, so return the array as is. |
k is larger than array length | Use the modulo operator (k % nums.length) to normalize k to be within the array's bounds. |
Array with one element | If the array has only one element, no rotation changes anything; return as is. |
Array with two elements and k=1 | A single rotation will simply swap the two elements; ensure code handles this basic case. |
Array with all identical values | The rotation should still work correctly regardless of the input value. |
Large array size and large k value | Ensure the algorithm is efficient in both space and time to avoid exceeding memory limits or causing excessive runtime. |
Negative k value | Handle negative k values appropriately; this typically requires adjusting the k value to represent a left rotation. |