Given an integer array arr and a positive integer d, left-rotate (counter-clockwise) the array by d steps and return the result.
A left rotation by one step moves every element one slot toward the front; the element that falls off the front wraps around to the back. Doing this d times, the element at index i ends up at index (i - d) mod n — equivalently, the result at index i is the original arr[(i + d) mod n].
Treat the array as circular, and note that d may be larger than the length: rotating by n leaves the array unchanged, so a rotation by d is the same as a rotation by d mod n. For example, arr = [1, 2, 3, 4, 5] with d = 2 becomes [3, 4, 5, 1, 2].
d sends the element at index i to index (i - d) mod n. Equivalently, the result at index i is the original arr[(i + d) mod n].d can be far bigger than n. Since rotating by n changes nothing, first reduce d to d mod n.d elements, reverse the rest, then reverse the whole array — and watch it snap into place.d mod n the rotation amount is 0, so the array is unchanged. Both approaches handle this automatically.d equals a left rotation by n - (d mod n), so the same reversal trick applies.Input: arr = [1, 2, 3, 4, 5], d = 2 Output: [3, 4, 5, 1, 2] Each element shifts 2 places left; the first 2 wrap to the end.
Input: arr = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20], d = 3 Output: [8, 10, 12, 14, 16, 18, 20, 2, 4, 6] The first 3 elements move to the back after a 3-step left rotation.
Input: arr = [7, 3, 9, 1], d = 9 Output: [3, 9, 1, 7] d is larger than the length: 9 mod 4 = 1, so this is a single left rotation.
- 1 <= arr.size <= 10^5 - 1 <= d <= 10^5 - 0 <= arr[i] <= 10^5
Rotating an array by d is the problem that introduces the three-reversal trick — a genuinely surprising way to swap two adjacent blocks in place, with no extra array. It also drills a habit worth keeping: reduce d modulo n before you touch anything. The brute force copies with an index formula; the optimal reverses three ranges.
d mod n collapses any rotation amount into the range 0 .. n-1, since rotating by n changes nothing.Given an array arr and a positive integer d, left-rotate by d and return the result. Treat the array as circular: since a full rotation of n is a no-op, rotating by d equals rotating by d mod n.
Worked example — arr = [1, 2, 3, 4, 5], d = 2
[1 2 | 3 4 5] split after the first d = 2 block A block B [3 4 5 | 1 2] swap the two blocks → answer ✓
“Is this a left or a right rotation?”
Left (counter-clockwise) here: [1,2,3,4,5] by 2 gives [3,4,5,1,2]. Confirming direction avoids a mirror-image bug.
“Can d be larger than n?”
Yes. Reduce it with d mod n first, or you'll index out of bounds.
“What if d is a multiple of n?”
Then d mod n is 0 and the array is unchanged — make sure your code returns it as-is.
“In place, or a new array?”
Any correct result is accepted, but the in-place O(1)-space version is the interesting one an interviewer pushes for.
“How large can the array be?”
Up to 10^5. Both approaches are O(n) time; the reversal trick wins on space by avoiding a second array.
A few checks before I code.
Is this a left rotation, so the first d elements move to the back?
Can d exceed n — should I reduce it mod n first?
Do you want it in place with O(1) extra space?
Then I'll use the three-reversal method after taking d mod n.
Rotating by n returns the array to itself, so only d mod n matters. Doing this reduction up front turns a possibly-huge d into a valid split point and makes the d = n case (a no-op) fall out automatically.
Left-rotating by d splits the array into block A (arr[0..d-1]) and block B (arr[d..n-1]) and swaps them to B A. Reframing "rotate" as "swap two adjacent blocks" is what unlocks the in-place trick.
Reverse block A, reverse block B, then reverse the whole array. The double-flip cancels within each block and leaves them in swapped order — A B → B A — using only in-place swaps, so O(1) space. This identity (reverse(reverse(A) + reverse(B)) = B + A) is the heart of the solution.
| Extra array | Three reversals | |
|---|---|---|
| Idea | result[i] = arr[(i + d) % n] | Reverse A, reverse B, reverse all |
| Time | O(n) | O(n) |
| Space | O(n) | O(1) |
The full code for both is in the Approaches selector below.
Key takeaway
Reduce d to d mod n, then treat the rotation as swapping two adjacent blocks. The three-reversal identity — reverse each block, then reverse the whole array — performs that swap in place, giving O(n) time and O(1) space. The same trick rotates strings and swaps line blocks in text editors.