Rotate Array – 189. LeetCode


The “Rotate Array” problem on LeetCode asks us to rotate an array of integers to the right by a given number of steps  k . This means each element in the array moves  k  positions to the right, with elements at the end wrapping around to the beginning. The challenge is to achieve this with minimal time and space complexity.


Optimal Algorithm and Data Structure Choice

For this problem, the Reversal Algorithm offers the lowest time and space complexity. This algorithm is based on reversing parts of the array in place without extra memory, achieving O(n) time complexity and O(1) space complexity.

Data Structure: Array

Algorithm Pattern: Reversal Pattern

The Reversal Algorithm is well-suited here because it operates directly on the array in a few simple operations (reversing parts of the array) without needing extra space or complex data structures.


Algorithm Steps

1. Normalize  k : Set  k = k \% n  (where  n  is the length of the array) to avoid unnecessary rotations.

2. Reverse Entire Array: Reverse all elements to set up the new arrangement.

3. Reverse First  Elements: Reverse the first  k  elements to position them correctly.

4. Reverse Remaining Elements: Reverse the rest of the array to complete the rotation.

public class RotateArray {
public void rotate(int[] nums, int k) {
// Normalize k to avoid unnecessary rotations
k = k % nums.length;

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

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

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

// Helper function to reverse a portion of the array
private void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
}

Line-By-Line In-Depth Explanation

Step 1: Normalizing K

k = k % nums.length;
  • Since rotating the array by its length (or any multiple of it) brings the array back to its original state, we only need to rotate by k % nums.length. This normalization step helps avoid unnecessary rotations.
  • This is an optimization to ensure we handle only the rotations needed, keeping the solution efficient.

Step 2: Reverse The Entire Array

reverse(nums, 0, nums.length - 1);
  • Here, we reverse the entire array, making the end elements come to the front and the front elements move to the end. This reversal sets up the array for its rotated state by bringing elements that should end up near the start of the rotated array closer to the start.

Step 3: Reverse The First k Elements

reverse(nums, 0, k - 1);
  • This line reverses only the first k elements in the array. After the initial full reversal, these elements are now in the positions they’ll occupy after the rotation, but in reverse order. This step restores the intended order of these k elements.

Step: 4 Reversing the Remaining Elements

reverse(nums, k, nums.length - 1);


This line reverses the part of the array from index k to the end. After the first two steps, this section needs to be ordered correctly to complete the rotation.

Step 5: Reverse Helper Function

private void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
  • The reverse method reverses any given portion of the array. It takes two indices, start and end, and swaps elements from these endpoints towards the middle. This continues until start meets or surpasses end.

Why This Code Works


By performing targeted reversals, this method rearranges the array efficiently. It avoids brute-force shifting or extra memory usage, aligning with a common array manipulation pattern: in-place, low-memory transformations. This approach exemplifies how identifying reversible operations can lead to optimized solutions for similar array manipulation problems.

Leave a Reply

Your email address will not be published. Required fields are marked *