Problem Description
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]
Python Solution
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
n = len(nums)
k = k % n # Handle k > n case
def reverse(start: int, end: int) -> None:
while start < end:
nums[start], nums[end] = nums[end], nums[start]
start += 1
end -= 1
# Reverse entire array
reverse(0, n - 1)
# Reverse first k elements
reverse(0, k - 1)
# Reverse remaining elements
reverse(k, n - 1)
Java Solution
class Solution {
public void rotate(int[] nums, int k) {
int n = nums.length;
k = k % n; // Handle k > n case
reverse(nums, 0, n - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, n - 1);
}
private void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
}
C++ Solution
class Solution {
public:
void rotate(vector& nums, int k) {
int n = nums.size();
k = k % n; // Handle k > n case
reverse(nums.begin(), nums.end());
reverse(nums.begin(), nums.begin() + k);
reverse(nums.begin() + k, nums.end());
}
};
JavaScript Solution
/**
* @param {number[]} nums
* @param {number} k
* @return {void} Do not return anything, modify nums in-place instead.
*/
var rotate = function(nums, k) {
const n = nums.length;
k = k % n; // Handle k > n case
const reverse = (start, end) => {
while (start < end) {
[nums[start], nums[end]] = [nums[end], nums[start]];
start++;
end--;
}
};
reverse(0, n - 1);
reverse(0, k - 1);
reverse(k, n - 1);
};
C# Solution
public class Solution {
public void Rotate(int[] nums, int k) {
int n = nums.Length;
k = k % n; // Handle k > n case
Reverse(nums, 0, n - 1);
Reverse(nums, 0, k - 1);
Reverse(nums, k, n - 1);
}
private void Reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
}
Complexity Analysis
- Time Complexity: O(n) where n is the length of the input array
- Space Complexity: O(1) as we modify the array in-place
Solution Explanation
This solution uses the reversal algorithm:
- First, we handle the case where k > n by using k = k % n
- Then we perform three reversals:
- Reverse the entire array
- Reverse the first k elements
- Reverse the remaining elements
Why this works:
- When we rotate an array by k positions, we're essentially moving the last k elements to the front
- The reversal algorithm achieves this by:
- First reversing the entire array to get the last k elements at the start
- Then reversing the first k elements to get them in the correct order
- Finally reversing the remaining elements to get them in the correct order