↑ Top

Solve Leetcode Rotate An Array To The Right By K Steps

ullas kunder

Designer & Developer

🧠 Rotate an Array to the Right by k Steps

βœ… Problem Statement

Given an array arr and an integer k, rotate the array to the right by k steps.

Rotation example:

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

This problem helps understand in-place array manipulation, reverse techniques, and modular arithmetic.

πŸ” Approach: Reverse Method (Optimal, O(n) time, O(1) space)

What: Use the reverse trick:

  1. Reverse the whole array.
  2. Reverse the first k elements.
  3. Reverse the remaining n-k elements.

Why: Efficient in-place solution, no extra array needed. How: Reversing rearranges elements systematically to achieve rotation.

🐍 Python Code

def rotate_right(arr, k):
    n = len(arr)
    k %= n

    def reverse(start, end):
        while start < end:
            arr[start], arr[end] = arr[end], arr[start]
            start += 1
            end -= 1

    # Step 1: Reverse the whole array
    reverse(0, n-1)

    # Step 2: Reverse first k elements
    reverse(0, k-1)

    # Step 3: Reverse remaining elements
    reverse(k, n-1)

    return arr

# Example
arr = [1, 2, 3, 4, 5]
print(rotate_right(arr, 2))  # Output: [4, 5, 1, 2, 3]

πŸ§ͺ JavaScript Code

function rotateRight(arr, k) {
  k %= arr.length;

  const reverse = (start, end) => {
    while (start < end) {
      [arr[start], arr[end]] = [arr[end], arr[start]];
      start++;
      end--;
    }
  };

  // Step 1: Reverse the whole array
  reverse(0, arr.length - 1);

  // Step 2: Reverse first k elements
  reverse(0, k - 1);

  // Step 3: Reverse remaining elements
  reverse(k, arr.length - 1);

  return arr;
}

// Example
console.log(rotateRight([1, 2, 3, 4, 5], 2)); // Output: [4, 5, 1, 2, 3]

πŸ”„ Execution Trace / Step-by-Step Example

Let’s rotate [1, 2, 3, 4, 5] by k = 2:

  1. Step 0: Original array
[1, 2, 3, 4, 5]
  1. Step 1: Reverse the whole array
reverse(0, 4) β†’ [5, 4, 3, 2, 1]
  1. Step 2: Reverse first k elements (0 to k-1)
reverse(0, 1) β†’ [4, 5, 3, 2, 1]
  1. Step 3: Reverse remaining elements (k to n-1)
reverse(2, 4) β†’ [4, 5, 1, 2, 3]

βœ… Final rotated array: [4, 5, 1, 2, 3]

πŸ” Breaking It Down for Understanding

  1. Reverse Concept: Reversing rearranges elements in the opposite order.
  2. Three-Step Trick: Reverse full β†’ reverse first k β†’ reverse rest β†’ achieves rotation.
  3. Modular Arithmetic: k %= n handles cases when k > n.
  4. In-Place Advantage: No extra space needed β†’ O(1) space.
  5. Pattern Recognition: Useful for rotations, cyclic shifts, and array transformations.

πŸ“ Summary

ApproachTime ComplexitySpace ComplexityNotes
Reverse MethodO(n)O(1)Optimal, in-place, uses three reverses

Tip: Whenever you need to rotate arrays efficiently, think of reversing segments β€” it often avoids extra arrays and keeps the solution clean.