![]() |
VOOZH | about |
Rotations in the array is defined as the process of rearranging the elements in an array by shifting each element to a new position. This is mostly done by rotating the elements of the array clockwise or counterclockwise.
There are two types of rotation.
Here, The array elements are shifted towards the right.
Here, The array elements are shifted towards the left.
In this article, we will discuss about right rotation of the array. You can refer to Left rotate an array by d positions to know about the left rotation of the array.
Here we consider right rotation for consistency, the left rotation can also be implemented using the same algorithms.
Input: arr[] = {1, 2, 3, 4, 5, 6}, d = 2
Output: {5, 6, 1, 2, 3, 4}
Explanation: After first right rotation, arr[] becomes {6, 1, 2, 3, 4, 5} and after the second rotation, arr[] becomes {5, 6, 1, 2, 3, 4}Input: arr[] = {1, 2, 3}, d = 4
Output: {3, 1, 2}
Explanation: The array is rotated as follows:
- After first right rotation, arr[] = {3, 1, 2}
- After second right rotation, arr[] = {2, 3, 1}
- After third right rotation, arr[] = {1, 2, 3}
- After fourth right rotation, arr[] = {3, 1, 2}
Following are different approaches to implement rotation.
At each iteration, shift the elements by one position to the right in a circular fashion (the last element becomes the first). Perform this operation d times to rotate the elements to the right by d positions.
Illustration:
Let us take arr[] = {1, 2, 3, 4, 5, 6}, d = 2.
First Step:
=> Rotate to right by one position.
=> arr[] = {6, 1, 2, 3, 4, 5}
Second Step:
=> Rotate again to right by one position
=> arr[] = {5, 6, 1, 2, 3, 4}
Rotation is done 2 times.
So the array becomes arr[] = {5, 6, 1, 2, 3, 4}
5 6 1 2 3 4
Time Complexity: O(n * d)
Auxiliary Space: O(1)
The idea is to use a temporary array of size n, where n is the length of the original array. If we right rotate the array by d positions, the last d elements will be in the beginning and the first (n - d) elements will be at the end.
- Copy the last delements of the original array into the first d positions of the temporary array
- Then copy the first n - d elements of the original array to the end of temporary array.
- Finally, copy all the elements of temporary array back into the original array.
Illustration for Right Rotation by 2 positions:
Below is the implementation of the above approach
5 6 1 2 3 4
Time complexity: O(n), where n is the size of input array arr[].
Auxiliary Space: O(n)
The idea behind Juggling Algorithm is that instead of moving one by one, we can use the concept of cycles. Each cycle is independent and represents a group of elements that will shift among themselves during the rotation. If the starting index of a cycle is i, then the next elements will be present at indices (i + d) % n, (i + 2d) % n, (i + 3d) % n ... and so on till we reach back to index i.
So for any index i, we know that element at index i will move to index (i + d) % n. Now, we can simply rotate all elements in the same cycle without interfering with any other cycle.
Working of the above algorithm:
Below is the implementation of the algorithm:
5 6 1 2 3 4
The idea is based on the observation that if we right rotate the array by d positions, the last d elements will be in the front and first (n - d) elements will be at the end.
- First reverse all the elements of the array.
- Then reverse first d elements.
- Finally, reverse last (n - d) elements to get the final rotated array.
Illustration:
Below is the implementation of the above approach:
5 6 1 2 3 4
Time Complexity: O(n)
Auxiliary Space: O(1)
Related Articles: