1. Problem Summary
You are given an integer array nums and an integer k.
Your task is to rotate the array to the right by k steps, where:
- Each step shifts elements one position to the right
- The last element wraps around to the front
Important details:
- Rotation must be done in-place
- Extra space usage should ideally be O(1)
kmay be larger than the array length
Example interpretation:
If nums = [1,2,3,4,5,6,7] and k = 3, the result becomes:
[5, 6, 7, 1, 2, 3, 4]
2. Key Insights
Rotations wrap around modulo array length
If array length = N:
Effective rotation = k % N
In-place rotation without using extra array
A clever reversal strategy makes this possible.
Reversal method is optimal
To rotate right by k:
- Reverse entire array
- Reverse first k elements
- Reverse remaining n–k elements
Example:
Original: 1 2 3 4 5 6 7 Reverse all: 7 6 5 4 3 2 1 Reverse k: 5 6 7 4 3 2 1 Reverse rest 5 6 7 1 2 3 4
Why reversal works
Because reversing segments restores left-to-right ordering after rotation.
3. Approach
In-place reversal strategy
Steps:
- Compute
k = k % nums.length - Reverse the entire array
- Reverse sub-array from index 0 to k–1
- Reverse sub-array from index k to n–1
Helper function reverse(nums, left, right) swaps elements inward.
This meets both time and space constraints.
4. Time and Space Complexity
Time Complexity: O(N)
Each element participates in a constant number of swaps.
Space Complexity: O(1)
No additional storage used.
5. Java Solution
class Solution {
public void rotate(int[] nums, int k) {
int n = nums.length;
k = k % n; // handle large k
reverse(nums, 0, n - 1); // Step 1: reverse all
reverse(nums, 0, k - 1); // Step 2: reverse first k
reverse(nums, k, n - 1); // Step 3: reverse remaining
}
private void reverse(int[] nums, int left, int right) {
while (left < right) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
left++;
right--;
}
}
}
6. Dry Run Examples
Example 1
Input:
nums = [1,2,3,4,5,6,7] k = 3
Step-by-step:
- Normalize k:
k = 3
- Reverse whole array:
[7,6,5,4,3,2,1]
- Reverse first k elements (3 elements):
[5,6,7,4,3,2,1]
- Reverse remaining elements:
[5,6,7,1,2,3,4]
Final result:
[5,6,7,1,2,3,4]
Example 2
Input:
nums = [-1, -100, 3, 99] k = 2
Normalize:
k = 2
Reverse all:
[99, 3, -100, -1]
Reverse first 2:
[3, 99, -100, -1]
Reverse last 2:
[3, 99, -1, -100]
Output:
[3, 99, -1, -100]
Example 3 (edge case)
Input:
nums = [1,2] k = 5
Normalize:
k = 5 % 2 = 1
Reverse all:
[2,1]
Reverse first 1:
no change
Reverse last segment:
no change
Output:
[2,1]
7. Why This Solution Works
- Avoids shifting elements one by one (too slow)
- Uses mathematical modulo reduction
- Reversal technique creates proper ordering efficiently
- Meets in-place requirement
- Works for all k sizes
- Handles small and large arrays uniformly
8. Common Mistakes
- Rotating by repeatedly shifting elements (O(N*k) time)
- Forgetting to apply
k % length - Using extra array when not allowed
- Incorrect reversal boundaries
- Confusing left rotation vs right rotation
