rotateArray.md
May 22, 2025 · View on GitHub
Description
Given an integer array nums, rotate the array to the right by n steps, where n is a non-negative number. The rotation is performed in-place, meaning no additional array is used for the operation.
Examples
Example 1:
Input: nums = [1,2,3,4,5,6,7], n = 4 Output: [4,5,6,7,1,2,3]
Example 2:
Input: nums = [-10, 4, 5, -1], n = 2 Output: [5, -1, -10, 4]
Example 3 (Edge Case):
Input: nums = [1], n = 10 Output: [1] (Single element array remains unchanged regardless of rotation count)
Algorithm overview: This problem can be solved using two different approaches:
Approach 1: Reversal Technique (Efficient)
This algorithm leverages the reversal technique (a two-pointer approach) to efficiently rotate the array in-place.
To rotate the array to the right by n steps:
- Reverse the entire array.
- Reverse the first
nelements. - Reverse the remaining
length - nelements.
Detailed steps:
-
Calculate Array Length
Initialize a variablelengthto store the total number of elements in the array. -
Handle Edge Cases No rotation is required when:
- The array is empty
- The rotation count is 0
- The rotation count is equal to the array length (full rotation returns to original state) This condition is helpful for early exit.
-
Normalize the Rotation Count
Update the rotation count by takingn % lengthto handle cases wherenexceeds the array size. This ensures we perform only the necessary rotations. -
Define a Reversal Helper Function
Implement a helper function that reverses elements between two indices (startandend) in the array using a two-pointer technique. Swap the elements atstartandend, then incrementstartand decrementendin each iteration until both pointers meet. -
Reverse the Entire Array
Apply the reversal function to the full array (from index0tolength - 1). This step brings the elements that need to be rotated to the front, but in reverse order. -
Reverse the First
nElements
Reverse the firstnelements (from index0ton - 1) to restore them to the correct order. -
Reverse the Remaining Elements
Reverse the remaining elements (from indexntolength - 1) to place the original beginning of the array back in the correct order. -
Return the Rotated Array
The array has now been successfully rotated in-place and is ready for use.
Approach 2: Brute Force Method
This approach uses JavaScript's array methods to perform the rotation one step at a time.
Detailed steps:
-
Calculate Array Length
Initialize a variablelengthto store the total number of elements in the array. -
Handle Empty Array Return immediately if the array is empty.
-
Normalize the Rotation Count
Update the rotation count by takingn % lengthto handle cases wherenexceeds the array size. -
Perform Rotation
For each of thensteps:- Remove the last element of the array using
pop() - Insert this element at the beginning of the array using
unshift()
- Remove the last element of the array using
-
Array is Rotated
Afterniterations, the array has been rotated to the right bynpositions.
Time and Space Complexity
Approach 1 (Reversal Technique):
-
Time Complexity:
O(n)
The algorithm performs three reverse operations, each takingO(n)time in total. -
Space Complexity:
O(1)
The rotation is done in-place, using only a constant amount of extra space regardless of input size.
Approach 2 (Brute Force):
-
Time Complexity:
O(n²)
For each of thenrotations, theunshift()operation takesO(n)time as it requires shifting all elements. -
Space Complexity:
O(1)
The rotation is done in-place, modifying the original array.