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
Array Rotation in-place: Three-Step Reversal Algorithm (n=7, k=3) Scale & Modulo: k = 10 is redundant! k = 10 % 7 = 3. Avoids 70 naive shifts by using 3 linear reversals. Initial nums = 1 2 3 4 5 6 7 | A (n-k=4) | B (k=3) | The Three-Step In-Place Reversal Algorithm: 1. Reverse Entire Array (0 to n-1) 7 6 5 4 3 2 1 Target elements [5,6,7] now front 2. Reverse first k elements (0 to k-1) 5 6 7 4 3 2 1 First 3 elements sorted! 3. Reverse remaining n-k (k to n-1) 5 6 7 1 2 3 4 Final rotated array! Time Complexity: O(n) | Space Complexity: O(1) auxiliary | Zero extra allocations!
Rotate [1, 2, 3, 4, 5, 6, 7] by k = 3 and by k = 10 overview diagram
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.
Why Shifting Array Elements One by One Fails at Scale Array length n = 7 | Comparing k = 3 vs massive k = 10 1. Normal Rotation (k = 3) Efficient 3 single-step shifts yield target array Original [1,2,3,4,5,6,7]: 1 2 3 4 5 6 7 k = 3 shifts Result [5,6,7,1,2,3,4]: 5 6 7 1 2 3 4 Small k works fine 3 steps = 21 total item movements. O(n × k) time complexity is manageable. 2. Massive Rotation (k = 10) — The Scaling Trap Naive Shifting 10 Times • Inner loop runs 10 separate times • Moves 70 individual items (7 items × 10) • Massive redundant shifting cycles! Inefficient: O(n × k) = 70 operations Smart Approach: k mod n • Array length n = 7 • Effective rotation: 10 mod 7 = 3 steps • Rotating 10 times is identical to 3! Optimal: O(n) time via Reversal Algorithm
Why shifting elements one by one fails at scale diagram
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
The 3-Step Reversal Model Behind Array Rotation Example: nums = [1, 2, 3, 4, 5, 6, 7] with k = 3 (n = 7, k = k % n) Initial Array & Segments (Segment A: n-k = 4 elements | Segment B: k = 3 elements) 1 2 3 4 5 6 7 Segment A (len 4) Segment B (len 3) Step 1 Reverse Entire Array [0 ... n-1] B moves to the front, A moves to the back (Ba, Ab). Both internal segments are temporarily reversed. 7 6 5 4 3 2 1 Step 2 Reverse First k Elements [0 ... k-1] Restores correct internal order for Segment B [5, 6, 7] at the front of the array. 5 6 7 4 3 2 1 Step 3 Reverse Remaining n-k Elements [k ... n-1] Restores correct internal order for Segment A [1, 2, 3, 4]. Final rotated array is achieved! 5 6 7 1 2 3 4 Time Complexity: O(n) | Space Complexity: O(1) | Handles large k (> n) via k = k % n
The 3-Step Reversal Model Behind Array Rotation diagram
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.
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.
Step 3: Reverse the remaining elements (indices to ).
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

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

Tracing the 3-Step Reversal Model (n=7, k=3) Initial: [1, 2, 3, 4, 5, 6, 7] • k = 3 0. Initial Array 1 2 3 4 5 6 7 Step 1: Reverse All 7 6 5 4 3 2 1 [5,6,7] now at left (backwards) Step 2: Rev First k(3) 5 6 7 4 3 2 1 Leading 3 elements in exact order Step 3: Rev Tail (n-k) 5 6 7 1 2 3 4 Final Result: [5, 6, 7, 1, 2, 3, 4] Key Takeaway of the 3-Step Reversal Pattern: 1. Reverse entire array (flips order globally) • 2. Reverse first k elements • 3. Reverse remaining n-k elements O(1) extra space!
Tracing the 3-Step Reversal Model with [1, 2, 3, 4, 5, 6, 7] and k = 3 diagram
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.
python
def rotate_transfer_check():
    nums = [10, 20, 30, 40, 50]
    k = 12
    # Apply the 3-step reversal model here
    # Expected final state: [40, 50, 10, 20, 30]

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.

Keep learning