Rotate Array — Solution in Python, Java & C++
Rotate Array solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem on arrays and math.
Rotate Array is a medium coding problem on arrays, math and two pointers, asked in Mindtree, Razorpay and Virtusa interviews.
- Difficulty: Medium
- Topics: Arrays, Math, Two Pointers
- Asked at: Mindtree, Razorpay, Virtusa
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given an integer array nums, rotate the array to the right by k steps (k is non-negative) and return the result.
Example 1
Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
Explanation: Rotate right 3 times: [7,1,…], [6,7,1,…], [5,6,7,1,2,3,4].
Example 2
Input: nums = [-1,-100,3,99], k = 2
Output: [3,99,-1,-100]
Constraints
1 <= nums.length <= 30-100 <= nums[i] <= 1000 <= k <= 100
Follow-up: the classic in-place trick reverses the whole array, then each half.
Hints for Rotate Array
Hint 1
k can exceed the length — reduce it with k % n first.
Hint 2
The last k % n elements move to the front, everything else shifts right.
How to solve Rotate Array
Rotating right by k moves the element at index i to (i + k) % n. The elegant in-place recipe is three reversals: reverse the whole array, then reverse the first k elements, then reverse the rest. That lands every element in the right place with no extra array.
Approach
- Reduce
kmodulon— rotating bynis a no-op, andkmay exceed the length. - Reverse the entire array.
- Reverse the first
kelements. - Reverse the remaining
n - kelements.
Why it works
Reversing the whole array brings the last k elements to the front but in the wrong order, and leaves the first n - k at the back, also reversed. Reversing each of those two blocks restores their internal order, which is exactly the rotation. Since each reversal is its own inverse on its block, the composition is a pure cyclic shift — and it touches each element a constant number of times.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- Take
k % nfirst, or the block boundaries fall outside the array. kmay be0after the modulo, in which case the three reversals correctly cancel out.- The direction matters: this rotates right; rotating left needs
n - k. - Reverse the whole array first — reversing the halves first gives a different (wrong) permutation.
Reference solution
Python
from typing import List
def rotate(nums: List[int], k: int) -> List[int]:
n = len(nums)
s = k % n
return nums[n - s:] + nums[:n - s]Java
public static int[] rotate(int[] nums, int k) {
int n = nums.length;
int shift = k % n;
reverseRange(nums, 0, n - 1);
reverseRange(nums, 0, shift - 1);
reverseRange(nums, shift, n - 1);
return nums;
}
private static void reverseRange(int[] nums, int lo, int hi) {
while (lo < hi) {
int t = nums[lo];
nums[lo] = nums[hi];
nums[hi] = t;
lo++;
hi--;
}
}C++
void reverseRange(vector<int>& nums, int lo, int hi) {
while (lo < hi) {
int t = nums[lo];
nums[lo] = nums[hi];
nums[hi] = t;
lo++;
hi--;
}
}
vector<int> rotate(vector<int>& nums, int k) {
int n = (int) nums.size();
int shift = k % n;
reverseRange(nums, 0, n - 1);
reverseRange(nums, 0, shift - 1);
reverseRange(nums, shift, n - 1);
return nums;
}JavaScript
var rotate = function(nums, k) {
const n = nums.length;
const s = k % n;
return nums.slice(n - s).concat(nums.slice(0, n - s));
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Two Pointers Technique