This technique (solution) is a popular and efficient approach for rotating an array to the right without using any extra space. The three steps of reversing segments of the array achieve the rotation effectively.
The reversal algorithm is widely used because it has a time complexity of O(n) and a space complexity of O(1), making it an efficient way to rotate arrays in-place. It is a valuable technique in array manipulation problems and is often used in various programming interviews and coding challenges.
This solution, as it mentioned by the name, involves three main steps:
- Reverse the entire array.
- Reverse the first k elements of the array.
- Reverse the remaining n-k elements of the array.
By following these steps, the array is effectively rotated to the right by k positions. This technique is commonly used to solve array rotation problems when you want to perform the rotation in-place without using any additional memory.
The three-step reversal algorithm is a valuable technique to have in your toolkit as it allows you to efficiently manipulate arrays and perform rotations, which can be helpful in various programming scenarios and challenges.

Explanation
The idea of this technique is to reverse the elements in the array in three steps: Reverse the entire array, Reverse the first k elements of the array, and Reverse the remaining n-k elements of the array.
Step 0: Calculate the minimum movements
Before rotating the array, we first need to find the actual number of positions we need to move, which is the remainder of dividing the desired number of movements (k = 7) by the size of the array (n = nums.length). This ensures that if the number of movements is greater than the size of the array, we only move the necessary spaces instead of going through the array multiple times. In this case after the operation k = 2;
Step 1: Reverse the entire array
public static void rotateLeft(int[] nums, int k) {
int n = nums.length;
k = k % n; // This operation is to avoid doing extra movements.
// Invert the array
reverse(nums, 0, n - 1);
//...
}
public static void reverse(int[] nums, int start, int end) {
/* The idea is to reverse the values that indicates from the start and end,
* in other words switch the first element (start) with the final element (end)
*/
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
For this step, we will create a reverse method takes an array of numbers (nums) and two positions in the array (start and end), this method will swap the array elements using the positions provided by the start and end. After the swap is done, the method continue with the next elements, incrementing the start and decreasing the end positions.
In this first step the reverse method will have the next values, the array (nums), the start of the array (start) and the end of the array (end). so if we have the array, like [1, 2, 3, 4, 5], after the call we will have the array reversed like [5, 4, 3, 2, 1].
Here’s how it works:
- We have an array of numbers, like
[1, 2, 3, 4, 5], and in this step (Reverse the entire array) the array will be reversed. - The method uses a “two-pointer” approach with
startandendinitially set to the given positions (0 and nums.length - 1). - Then swaps the elements at
startandend. In our example, it would swap the value at position0with the value at position4(nums.length - 1). So, the array becomes[.5, 2, 3, 4, 1] - The method then moves the
startpointer one position to the right (incrementing in1), and theendpointer one position to the left (decrementing in1). This means it’s now looking at positions1and3. - The method swaps the elements at
startandendagain, but this time, they both point to the number 3. Since they’re the same, there’s no need to swap, and the array remains[5, 4, 3, 2, 1]. - The method continues this process until
startis greater than or equal toend. This happens when the pointers have crossed each other in the middle of the array.
Step 2: Reverse the entire array
The next step, is to call again the same reverse method with the reversed array that we obtain from the previous step, the start of the array (0) and the end this time will be the number of movements minus 1 (k - 1).
//...
// Step 1: Invert the array
reverse(nums, 0, n - 1);
// Step 2: Invert the first elements
reverse(nums, 0, k - 1);
//...
The reverse method will do the same as the first step but only will swaps the elements from 0 to k-1. Continue with the resultant array from the previous step [5, 4, 3, 2, 1], in the end we will have the array like [4, 5, 3, 2, 1].
Step 3: Reverse the entire array
The last step, is similar to the step 1 and step 2, but, we will send to the pointers the k value for start and n - 1 for the end.
//...
// Step 1: Invert the array
reverse(nums, 0, n - 1);
// Step 2: Invert the first elements
reverse(nums, 0, k - 1);
// Step 3: Invert the last elements
reverse(nums, k, n - 1);
//...
In this last call to the reverse method, we will do the same as the first and second step, but, this time we will change the last values that are pending in the array, this values are from k for start to n -1 to end. In the last step we finish with the array [4, 5, 3, 2, 1], after finish the last step we got the array like [4, 5, 1, 2, 3].
The three-step reversal algorithm is a valuable technique to have in your toolkit as it allows you to efficiently manipulate arrays and perform rotations, which can be helpful in various programming scenarios and challenges.
You can download the code from here.
Thanks for reading and Happy Learning!!!


Leave a comment