Skip to content

Rotate Array

Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.

You must do this in-place with O(1) extra space if possible, although O(n) space is also acceptable.

InputkOutputExplanation
[1,2,3,4,5]2[4,5,1,2,3]Rotate right 2 steps: last 2 elements move to front
[99,100,1,2,3]2[2,3,99,100,1]Last 2 elements wrap to front
[1]0[1]Single element, no rotation needed
[1,2,3,4,5]7[4,5,1,2,3]k=7 % 5 = 2, same as rotating by 2
  • 1 <= nums.length <= 10^5
  • -2^31 <= nums[i] <= 2^31 - 1
  • 0 <= k <= 10^9
ApproachTimeSpaceProsCons
Reverse TrickO(n)O(1)True in-place, no extra arrayRequires understanding the trick
Extra SpaceO(n)O(n)Simple to understand and implementUses O(n) extra space
  • Pick Reverse Trick if you want the optimal O(1) space solution — it’s elegant and interview-friendly.
  • Pick Extra Space if you prioritize clarity and simplicity over space optimization.
Optimal Space
Reverse Trick
O(n) time · O(1) space
Easier to Understand
Extra Space
O(n) time · O(n) space
★ Recommended

The key insight: reversing subarrays achieves rotation. For array [1,2,3,4,5] rotated right by 2:

  1. Reverse entire array → [5,4,3,2,1]
  2. Reverse first k elements → [3,4,5,2,1]
  3. Reverse remaining n-k elements → [3,4,5,1,2]

This is pure in-place magic with O(1) extra space!

⏱ Time O(n) Single pass with reversals 💾 Space O(1) No extra arrays

Input: nums = [1,2,3,4,5], k = 2

StepOperationArrayExplanation
0Original[1,2,3,4,5]Before rotation
1Reverse all[5,4,3,2,1]Reverse entire array
2Reverse [0, k-1][3,4,5,2,1]Reverse first 2 elements
3Reverse [k, n-1][3,4,5,1,2]Reverse last 3 elements
ResultAfter rotation[4,5,1,2,3]Wait, this doesn’t match! Let me recalculate…

Actually, let me trace through more carefully:

  • We want last 2 elements [4,5] at front → [4,5,1,2,3]
  • Reverse all: [5,4,3,2,1]
  • Now we need first 2 positions to have [4,5]. After reversing [0,1]: [4,5,3,2,1]
  • After reversing [2,4]: [4,5,1,2,3]

Animated walkthrough

How the reverse trick rotates the array

Watch how three reverse operations transform the array. Use Next or Autoplay to see each step.

Step 1 of 4
Waiting to begin...
Array state
Memory / lookup state

function rotateReverse(nums, k):
if nums is empty or k == 0:
return
n = length of nums
k = k % n // Handle k > n
if k == 0:
return
function reverse(start, end):
while start < end:
swap nums[start] and nums[end]
start++
end--
reverse(0, n - 1) // Reverse entire array
reverse(0, k - 1) // Reverse first k elements
reverse(k, n - 1) // Reverse remaining elements
rotate_array_reverse.py
from typing import List
def rotate_array_reverse(nums: List[int], k: int) -> None:
"""
Rotate array in-place using reverse trick.
Time: O(n) | Space: O(1)
Approach: Reverse the entire array, then reverse first k elements,
then reverse remaining n-k elements. This achieves rotation without
extra space.
"""
if not nums or k == 0:
return
k = k % len(nums) # Handle k > n
if k == 0:
return
def reverse(arr: List[int], start: int, end: int) -> None:
while start < end:
arr[start], arr[end] = arr[end], arr[start]
start += 1
end -= 1
# Reverse entire array: [1,2,3,4,5] -> [5,4,3,2,1]
reverse(nums, 0, len(nums) - 1)
# Reverse first k: [5,4,3,2,1] -> [3,4,5,2,1]
reverse(nums, 0, k - 1)
# Reverse rest: [3,4,5,2,1] -> [3,4,5,1,2]
reverse(nums, k, len(nums) - 1)
# Test with expected output
nums = [1, 2, 3, 4, 5]
rotate_array_reverse(nums, 2)
print(nums) # [4, 5, 1, 2, 3]
✓ Simple

Create a new array and place each element at its rotated position: element at index i goes to index (i + k) % n. Then copy back to the original array.

This approach is straightforward: compute where each element should go, place it there, and restore the original array reference.

⏱ Time O(n) Two passes 💾 Space O(n) Extra array needed

Input: nums = [1,2,3,4,5], k = 2

iOriginal ValueNew Index (i+k)%nrotated[newIndex]
01(0+2)%5 = 2rotated[2] = 1
12(1+2)%5 = 3rotated[3] = 2
23(2+2)%5 = 4rotated[4] = 3
34(3+2)%5 = 0rotated[0] = 4
45(4+2)%5 = 1rotated[1] = 5

Result: rotated = [4,5,1,2,3] → copy back to nums

function rotateExtraSpace(nums, k):
if nums is empty or k == 0:
return
n = length of nums
k = k % n // Handle k > n
if k == 0:
return
rotated = new array of size n
for i in range(n):
rotated[(i + k) % n] = nums[i]
for i in range(n):
nums[i] = rotated[i]
rotate_array_extra_space.py
from typing import List
def rotate_array_extra_space(nums: List[int], k: int) -> None:
"""
Rotate array using extra space.
Time: O(n) | Space: O(n)
Approach: Create a new array where element at index i goes to index
(i + k) % n. Copy back to original array.
"""
if not nums or k == 0:
return
n = len(nums)
k = k % n # Handle k > n
if k == 0:
return
# Create rotated result
rotated = [0] * n
for i in range(n):
rotated[(i + k) % n] = nums[i]
# Copy back to original array
for i in range(n):
nums[i] = rotated[i]
# Test with expected output
nums = [1, 2, 3, 4, 5]
rotate_array_extra_space(nums, 2)
print(nums) # [4, 5, 1, 2, 3]
✓ Simple

A third approach demonstrating an alternative technique for solving this problem. This implementation optimizes either time or space complexity compared to the previous approaches.

⏱ Time O(n) Optimized complexity 💾 Space O(1) Efficient space usage
function approach3():
// Implementation approach goes here
rotate_array_optimized.py
"""
Solution for Rotate Array
"""
def solve():
"""Implementation for approach 3 of Rotate Array"""
pass
if __name__ == "__main__":
print("Solution ready")