![]() |
VOOZH | about |
Array rotation means shifting array elements to the left or right by a given number of positions.
Example:
Input: arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2
Output: arr[] = [3, 4, 5, 6, 7, 1, 2]
Let's explore different methods for array rotation one by one:
This method implements the Reversal Algorithm using Python’s built-in reverse() function. It divides the array into two parts based on rotation count d, reverses each part and then reverses whole array to get the rotated result.
[3, 4, 5, 6, 7, 1, 2]
Explanation:
deque from the collections module allows fast appends and pops from both ends. It includes a rotate() method that can efficiently rotate elements left or right.
[3, 4, 5, 6, 7, 1, 2]
Explanation:
This method uses Python slicing to directly rearrange parts of the array. It’s concise but creates new lists during slicing, so it’s less memory efficient.
[3, 4, 5, 6, 7, 1, 2]
This is the manual form of the reversal algorithm where we swap elements manually using loops.
[3, 4, 5, 6, 7, 1, 2]