beginner6 min read·Updated September 18, 2026
Rotate Array by k Steps Explained: Walkthrough with [1,2,3,4,5,6,7]
Master array rotation with a clear mental model. Walk through rotating [1,2,3,4,5,6,7] by k=3 and k=10 step-by-step without extra memory.
By Learnisim AI·Published September 18, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
- Basic array indexing
- Basic understanding of functions and loops
Why
Why shifting elements one by one fails at scale
Imagine you are handed the array and told to rotate it to the right by steps. If you shift elements one step at a time, the last element moves to the front, and everything else shuffles over. Doing this once turns our array into . Doing it three times successfully yields . But what happens when we scale up to ? Shifting element-by-element 10 separate times requires moving 70 individual items, even though the array length means rotating by 10 is identical to rotating by steps. As grows larger than the array length, naive shifting wastes redundant operations, highlighting the need for a smarter way to handle array shifts without looping times.
Model
The 3-Step Reversal Model Behind Array Rotation
Instead of shifting individual elements of one position to the right times—which forces redundant movements—we can achieve the exact same final state by manipulating contiguous blocks of memory. The three-step reversal algorithm treats the array as two joined segments, and , where contains the first elements and contains the last elements. To swap their positions from to , we reverse the entire array first, then independently reverse the first elements, and finally reverse the remaining elements. When , and , and this geometric choreography reorganizes the elements in time without allocating a secondary array.
Try this: Given nums = [1, 2, 3, 4, 5, 6, 7] and k = 3:
1.Reverse entire array
2.Reverse first k elements
3.Reverse remaining n-k elements
Worked example
Tracing the 3-Step Reversal Model with [1, 2, 3, 4, 5, 6, 7] and k = 3
Now that we know the three-step reversal sequence, let us apply it directly to our locked working example: Given and .
Before manipulating any elements, we normalize using . Since and , , meaning we need to shift the final 3 elements to the front.
Step 1: Reverse the entire array.
We swap elements from the outer edges inward until the entire array is flipped.
* Initial:
* Result of Step 1:
Notice how the elements that belong at the front () are now clustered at the left, but they are backwards.
We swap elements from the outer edges inward until the entire array is flipped.
* Initial:
* Result of Step 1:
Notice how the elements that belong at the front () are now clustered at the left, but they are backwards.
Step 2: Reverse the first elements (indices 0 to ).
Our is 3, so we reverse the sub-array .
* Sub-array before:
* Result of Step 2:
Now our leading three elements are in their exact final sorted order.
Our is 3, so we reverse the sub-array .
* Sub-array before:
* Result of Step 2:
Now our leading three elements are in their exact final sorted order.
Step 3: Reverse the remaining elements (indices to ).
Our remaining elements are . We reverse this tail sub-array.
* Tail before:
* Tail after:
Our remaining elements are . We reverse this tail sub-array.
* Tail before:
* Tail after:
Final Result: . This matches our expected outcome, achieved with zero extra array allocations.
Try this: def rotate(nums, k):
n = len(nums)
k = k % n
while start < end:
nums[start], nums[end] = nums[end], nums[start]
start, end = start + 1, end - 1
n = len(nums)
k = k % n
Helper to reverse a section in-place
def reverse(start, end):while start < end:
nums[start], nums[end] = nums[end], nums[start]
start, end = start + 1, end - 1
TODO: Apply the 3-step reversal algorithm here
Apply
Applying the Reversal Pattern to New Sizes and Shift Counts
Now that we have successfully traced our 7-element array with and , let us test the limits of our 3-step reversal pattern on a completely different array size and rotation count. Suppose we are given a new array of length , specifically , and we need to rotate it by steps to the right. We first normalize the large shift count using modulo arithmetic: . Next, we apply our proven three-step recipe: reverse the entire array to get , reverse the first elements to get , and finally reverse the remaining elements. This structural transfer proves that the triple-reversal algorithm is independent of specific array values and scales cleanly to any positive integer length.
FAQ
How does rotating [1, 2, 3, 4, 5, 6, 7] by k = 3 yield [5, 6, 7, 1, 2, 3, 4]?
By applying the 3-step reversal algorithm: first reverse the entire array, then reverse the first k elements [7,6,5,4,3,2,1] into [5,6,7], and finally reverse the remaining elements.
Why do we use k % n when k is larger than the array length?
Since rotating an array by its exact length results in the original array, any shift count greater than the length repeats in cycles. Using k % n eliminates redundant full rotations.
What is the time and space complexity of the reversal algorithm?
The reversal algorithm runs in O(n) time because every element is visited a constant number of times during reversals, and O(1) auxiliary space since it operates entirely in-place.
Why is shifting elements one by one inefficient?
Shifting elements one position at a time for k steps results in O(n * k) time complexity, which times out for large arrays and large shift values.