[Medium] 189. Rotate Array
Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
Examples
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 <= 10^5-2^31 <= nums[i] <= 2^31 - 10 <= k <= 10^9
Thinking Process
Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
- Two indices move toward each other or in the same direction.
- Works on sorted arrays or when in-place modification is required.
- Loop invariant: all indices outside
[left, right]are already resolved.
Common Approaches
Typical techniques for this pattern:
| Approach | Time | Space | Notes |
|---|---|---|---|
| Opposite ends (this problem) | O(n) | O(1) | Sorted array pair search, reversal |
| Slow / fast pointers | O(n) | O(1) | Linked list middle, cycle detection |
| Same-direction chase | O(n) | O(1) | Remove duplicates in-place |
| Sliding window (variable) | O(n) | O(1) | Subarray with constraint |
Solution
This solution rotates the array by 1 step, k times.
class Solution:
def rotate(self, nums, k):
k %= len(nums)
for i in range(k):
previous = nums[len(nums) - 1]
for j in range(len(nums)):
temp = nums[j]
nums[j] = previous
previous = temp
Solution Explanation
Approach: Opposite ends (this problem)
Key idea: Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
How the code works:
- Two indices move toward each other or in the same direction.
- Works on sorted arrays or when in-place modification is required.
- Loop invariant: all indices outside
[left, right]are already resolved.
Walkthrough — input nums = [1,2,3,4,5,6,7], k = 3, expected output [5,6,7,1,2,3,4]:
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]
- Time Complexity: O(n × k) — For each of the
ksteps, we traversenelements. - Space Complexity: O(1) — Only a few extra variables.
This approach is simple but can be too slow when k and n are large.
Edge Cases
k = 0→ Array remains unchanged.kmultiple ofn→ Array remains unchanged after normalization withk %= n.- Single element array → Always unchanged.
- Large
k(e.g.,k > n) → Handled byk %= n.
Common Mistakes
- Skipping edge cases (empty input, single element, boundaries).
- Off-by-one errors in loops and index ranges.
- Forgetting to handle the case when no valid answer exists.
Related Problems
- LC 61. Rotate List
- LC 189. Rotate Array — This problem
- LC 396. Rotate Function
Key Takeaways
- Pattern: Opposite ends (this problem)
- Two indices move toward each other or in the same direction.
- Works on sorted arrays or when in-place modification is required.
References
- LC 189: Rotate Array on LeetCode
- LeetCode Discuss — LC 189: Rotate Array
- LeetCode Editorial (may require premium)