Learnitweb

LeetCode Problem 189 Rotate Array


1. Problem Summary

You are given an integer array nums and an integer k.
Your task is to rotate the array to the right by k steps, where:

  • Each step shifts elements one position to the right
  • The last element wraps around to the front

Important details:

  • Rotation must be done in-place
  • Extra space usage should ideally be O(1)
  • k may be larger than the array length

Example interpretation:
If nums = [1,2,3,4,5,6,7] and k = 3, the result becomes:

[5, 6, 7, 1, 2, 3, 4]

2. Key Insights

Rotations wrap around modulo array length

If array length = N:

Effective rotation = k % N

In-place rotation without using extra array

A clever reversal strategy makes this possible.

Reversal method is optimal

To rotate right by k:

  1. Reverse entire array
  2. Reverse first k elements
  3. Reverse remaining n–k elements

Example:

Original:    1 2 3 4 5 6 7
Reverse all: 7 6 5 4 3 2 1
Reverse k:   5 6 7 4 3 2 1
Reverse rest 5 6 7 1 2 3 4

Why reversal works

Because reversing segments restores left-to-right ordering after rotation.


3. Approach

In-place reversal strategy

Steps:

  1. Compute k = k % nums.length
  2. Reverse the entire array
  3. Reverse sub-array from index 0 to k–1
  4. Reverse sub-array from index k to n–1

Helper function reverse(nums, left, right) swaps elements inward.

This meets both time and space constraints.


4. Time and Space Complexity

Time Complexity: O(N)

Each element participates in a constant number of swaps.

Space Complexity: O(1)

No additional storage used.


5. Java Solution

class Solution {
    public void rotate(int[] nums, int k) {
        int n = nums.length;
        k = k % n;  // handle large k

        reverse(nums, 0, n - 1);     // Step 1: reverse all
        reverse(nums, 0, k - 1);     // Step 2: reverse first k
        reverse(nums, k, n - 1);     // Step 3: reverse remaining
    }

    private void reverse(int[] nums, int left, int right) {
        while (left < right) {
            int temp = nums[left];
            nums[left] = nums[right];
            nums[right] = temp;
            left++;
            right--;
        }
    }
}

6. Dry Run Examples

Example 1

Input:

nums = [1,2,3,4,5,6,7]
k = 3

Step-by-step:

  1. Normalize k:
k = 3
  1. Reverse whole array:
[7,6,5,4,3,2,1]
  1. Reverse first k elements (3 elements):
[5,6,7,4,3,2,1]
  1. Reverse remaining elements:
[5,6,7,1,2,3,4]

Final result:

[5,6,7,1,2,3,4]

Example 2

Input:

nums = [-1, -100, 3, 99]
k = 2

Normalize:

k = 2

Reverse all:

[99, 3, -100, -1]

Reverse first 2:

[3, 99, -100, -1]

Reverse last 2:

[3, 99, -1, -100]

Output:

[3, 99, -1, -100]

Example 3 (edge case)

Input:

nums = [1,2]
k = 5

Normalize:

k = 5 % 2 = 1

Reverse all:

[2,1]

Reverse first 1:
no change

Reverse last segment:
no change

Output:

[2,1]

7. Why This Solution Works

  • Avoids shifting elements one by one (too slow)
  • Uses mathematical modulo reduction
  • Reversal technique creates proper ordering efficiently
  • Meets in-place requirement
  • Works for all k sizes
  • Handles small and large arrays uniformly

8. Common Mistakes

  1. Rotating by repeatedly shifting elements (O(N*k) time)
  2. Forgetting to apply k % length
  3. Using extra array when not allowed
  4. Incorrect reversal boundaries
  5. Confusing left rotation vs right rotation