AlgoMaster Logo

Rotate Array

mediumFrequency9 min readUpdated June 23, 2026

Understanding the Problem

We need to shift every element in the array k positions to the right. Elements that "fall off" the right end wrap around to the beginning.

An important detail: k can be larger than the array length. Rotating an array of length n by n steps brings it back to the original, so we only care about k % n effective rotations. If k = 10 and n = 7, that's the same as rotating by 3.

The problem also asks us to modify the array in-place, which rules out creating and returning a new array. Using extra space as an intermediate step and copying back is still allowed, and we cover that as one approach.

Key Constraints:

  • 1 <= nums.length <= 10^5 → With n up to 100,000, an O(n^2) algorithm runs about 10 billion operations, too slow to finish in time. This rules out rotating one step at a time, whose cost approaches O(n^2) when k is close to n. We need an O(n) approach.
  • 0 <= k <= 10^5 → k can exceed n, so we normalize with k = k % n. k can also be 0, meaning no rotation at all.
  • -2^31 <= nums[i] <= 2^31 - 1 → Values span the full signed 32-bit range, but we only move elements around and never do arithmetic on them, so overflow is not a concern.

Approach 1: Brute Force (Rotate One by One)

Intuition

We can do exactly what the problem describes: rotate the array by one position, then repeat that k times. A single right rotation by one moves every element one slot to the right, and the last element wraps around to the front.

To perform one rotation, save the last element, shift every other element one position to the right, then place the saved element at index 0. After k such rotations, the array is rotated by k.

Algorithm

  1. Set k = k % n to handle cases where k is larger than the array length.
  2. Repeat k times:
    • Save the last element of the array.
    • Shift every element one position to the right (starting from the end).
    • Place the saved element at index 0.
  3. The array is now rotated by k steps.

Example Walkthrough

1Initial array. k=3, so we rotate one step at a time, 3 times.
0
1
1
2
2
3
3
4
4
5
5
6
6
7
1/7

Code

This repeats a lot of work. Every element moves k times, one position per rotation, even though its final destination is fixed at (i + k) % n from the start. Computing that destination once and writing the element there directly removes the intermediate shifts entirely.

Approach 2: Extra Array

Intuition

When we rotate right by k, the element at index i ends up at index (i + k) % n. The modulo handles the wraparound for elements that pass the right end. With that formula we can write each element straight to its final slot in a separate array, then copy the result back into nums.

Algorithm

  1. Set k = k % n.
  2. Create a temporary array of the same size.
  3. For each index i, place nums[i] at position (i + k) % n in the temporary array.
  4. Copy the temporary array back into nums.

Example Walkthrough

1temp initialized to empty. k=3, n=7
[_, _, _, _, _, _, _]
1/6

Code

This runs in O(n) time, but it uses a second array of size n. The mapping (i + k) % n is correct, yet the full copy is not strictly necessary. The next two approaches achieve the same rearrangement in O(1) extra space by working within the original array.

Approach 3: Cyclic Replacements (In-Place)

Intuition

The extra-array approach already knows the final position of every element: the element at index i belongs at (i + k) % n. The only reason it needed a second array was to avoid overwriting a value before it was moved.

We can avoid that conflict without a second array. Pick a starting index, save its value, then move it to its target. Doing so overwrites whatever was at the target, so save that value first and move it next. Following this chain of "evict, then place" steps traces a cycle that eventually returns to the starting index, with every element along the way landing in its final slot.

One cycle does not always cover the whole array. The chain returns to where it started after visiting n / gcd(n, k) elements, so there are gcd(n, k) separate cycles. To handle all of them, advance the start index by one each time a cycle closes and keep going until every element has been placed. A single counter of placed elements tells us when to stop.

Algorithm

  1. Set k = k % n. If k is 0, the array is already in place, so return.
  2. Keep a count of how many elements have been placed, starting at 0.
  3. For each cycle start start from 0 upward, while count < n:
    • Set current = start and hold prev = nums[start].
    • Repeat until the cycle returns to start:
      • Compute next = (current + k) % n.
      • Save nums[next], write prev into nums[next], then set prev to the saved value.
      • Move current to next and increment count.
  4. Stop once count equals n.

Example Walkthrough

1Original array. k=3, n=7. gcd(7,3)=1, so one cycle covers everything. start=0, count=0.
0
start, hold 1
1
1
2
2
3
3
4
4
5
5
6
6
7
1/9

For this input gcd(7, 3) is 1, so a single cycle touches all seven elements. When n and k share a factor, for example n=4 and k=2, gcd is 2 and the loop runs two cycles starting at index 0 and index 1, which is why the count check and the advancing start index are both needed.

Code

The cyclic approach matches the optimal time and space bounds, but tracking cycles and the eviction chain is harder to get right than three plain reversals. The next approach reaches the same O(n) time and O(1) space with simpler code.

Approach 4: Reverse Array (Optimal)

Intuition

A rotation splits the array into two blocks and swaps their order. With nums = [1, 2, 3, 4, 5, 6, 7] and k = 3, the result [5, 6, 7, 1, 2, 3, 4] is the last 3 elements [5, 6, 7] followed by the first 4 elements [1, 2, 3, 4]. The two blocks keep their internal order; only the blocks switch places.

Reversing the whole array gets both blocks into the front-vs-back arrangement we want, but with each block's elements running backward. Reversing [1, 2, 3, 4, 5, 6, 7] gives [7, 6, 5, 4, 3, 2, 1]. The first 3 slots now hold [7, 6, 5], which are the correct front-block values in reverse, and the last 4 slots hold [4, 3, 2, 1], the correct back-block values in reverse.

Reversing each block in place fixes the internal order. Reverse the first 3: [5, 6, 7]. Reverse the last 4: [1, 2, 3, 4]. The array is now [5, 6, 7, 1, 2, 3, 4], the rotated answer, produced with three reversals and no extra array.

Why the block boundary lands at index k: after a right rotation by k, the k elements that came from the tail occupy the first k slots, so the first reversal block is [0, k-1] and the second is [k, n-1].

Algorithm

  1. Set k = k % n to handle k larger than the array length.
  2. Reverse the entire array.
  3. Reverse the first k elements.
  4. Reverse the remaining n - k elements.

Example Walkthrough

1Original array. k=3, n=7. Plan: 3 reversals.
0
1
1
2
2
3
3
4
4
5
5
6
6
7
1/7

Code