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

Examples

Example 1:

Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:

Input: nums = [-1,-100,3,99], k = 2
Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]

Constraints

  • 1 <= nums.length <= 10^5
  • -2^31 <= nums[i] <= 2^31 - 1
  • 0 <= k <= 10^9

Thinking Process

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

  • Two indices move toward each other or in the same direction.
  • Works on sorted arrays or when in-place modification is required.
  • Loop invariant: all indices outside [left, right] are already resolved.
Two pointers 1 3 5 7 9 L R move L/R based on comparison

Common Approaches

Typical techniques for this pattern:

Approach Time Space Notes
Opposite ends (this problem) O(n) O(1) Sorted array pair search, reversal
Slow / fast pointers O(n) O(1) Linked list middle, cycle detection
Same-direction chase O(n) O(1) Remove duplicates in-place
Sliding window (variable) O(n) O(1) Subarray with constraint

Solution

This solution rotates the array by 1 step, k times.

class Solution:
    def rotate(self, nums, k):
        k %= len(nums)
        
        for i in range(k):
            previous = nums[len(nums) - 1]
            
            for j in range(len(nums)):
                temp = nums[j]
                nums[j] = previous
                previous = temp

Solution Explanation

Approach: Opposite ends (this problem)

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

How the code works:

  • Two indices move toward each other or in the same direction.
  • Works on sorted arrays or when in-place modification is required.
  • Loop invariant: all indices outside [left, right] are already resolved.

Walkthrough — input nums = [1,2,3,4,5,6,7], k = 3, expected output [5,6,7,1,2,3,4]:

rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4]

  • Time Complexity: O(n × k) — For each of the k steps, we traverse n elements.
  • Space Complexity: O(1) — Only a few extra variables.

This approach is simple but can be too slow when k and n are large.

Edge Cases

  1. k = 0 → Array remains unchanged.
  2. k multiple of n → Array remains unchanged after normalization with k %= n.
  3. Single element array → Always unchanged.
  4. Large k (e.g., k > n) → Handled by k %= n.

Common Mistakes

  • Skipping edge cases (empty input, single element, boundaries).
  • Off-by-one errors in loops and index ranges.
  • Forgetting to handle the case when no valid answer exists.

Key Takeaways

  • Pattern: Opposite ends (this problem)
  • Two indices move toward each other or in the same direction.
  • Works on sorted arrays or when in-place modification is required.

References

Template Reference