Open In App

Python Program for Reversal algorithm for array rotation

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
26 Likes
Like
Report

Write a function rotate(arr[], d, n) that rotates arr[] of size n by d elements. In this article, we will explore the Reversal Algorithm for array rotation and implement it in Python.

Example

Input:  arr[] = [1, 2, 3, 4, 5, 6, 7]
         d = 2
Output: arr[] = [3, 4, 5, 6, 7, 1, 2] 
Array

Rotation of the above array by 2 will make an array

ArrayRotation1

Python Program for Reversal Algorithm of Array Rotation

# Function to reverse arr[]
def rverseArray(arr,d):
    c=(arr[d:])+(arr[:d])
    return c
# Driver function to test above functions
arr = [1, 2, 3, 4, 5, 6, 7]
d=2
print(rverseArray(arr,d))

Output
[3, 4, 5, 6, 7, 1, 2]

Python Program for Reversal algorithm for Array Rotation


Output
3
4
5
6
7
1
2

Python Program for Reversal Algorithm Using collections.deque


Output
Rotated array: [3, 4, 5, 6, 7, 1, 2]

Time complexity: O(n + k).
Auxiliary space: O(n)


Article Tags :
Practice Tags :

Similar Reads