Python Program for Program to cyclically rotate an array by one
Last Updated :
18 Oct, 2023
Write a Python program for a given array, the task is to cyclically rotate the array clockwise by one time.
Examples:
Input: arr[] = {1, 2, 3, 4, 5}
Output: arr[] = {5, 1, 2, 3, 4}
Input: arr[] = {2, 3, 4, 5, 1}
Output: {1, 2, 3, 4, 5}
Approach:
Assign every element with its previous element and first element with the last element .
Illustrations:
Consider an array: arr[] = {1, 2, 3, 4, 5}
- Initialize last element in variable ‘last_el’ that is 5
- Then, iterate from n-1 to 1 and assign arr[i] = arr[i-1]
- arr[4] = arr[3]
- arr[3] = arr[2]
- arr[2] = arr[1]
- arr[1] = arr[0]
- Assign arr[0] = last_el
- Thus the required array will be {5, 1, 2, 3, 4}
Step-by-step approach:
- Store the last element in any temp variable
- Shift every element one position ahead
- Assign first value = last value (stored in temp variable).
Below is the implementation of the above approach:
Python3
# Python3 code for program to
# cyclically rotate an array by one
# Method for rotation
def rotate(arr, n):
last_el = arr[n - 1]
for i in range(n - 1, 0, -1):
arr[i] = arr[i - 1]
arr[0] = last_el
# Driver function
arr = [1, 2, 3, 4, 5]
n = len(arr)
print("Given array is")
for i in range(0, n):
print(arr[i], end=' ')
rotate(arr, n)
print("\nRotated array is")
for i in range(0, n):
print(arr[i], end=' ')
# This article is contributed
# by saloni1297
OutputGiven array is
1 2 3 4 5
Rotated array is
5 1 2 3 4
Time Complexity: O(n), Where n is the number of elements in the array.
Auxiliary Space: O(1), as we are using constant space.
Python Program for Program to cyclically rotate an array by one using Two Pointers Technique:
We can use two pointers, As we know in cyclic rotation we will bring last element to first and shift rest in forward direction, we can do this by swapping every element with last element till we get to the last point.
Step-by-step approach:
- Take two pointers i and j which point to the first and last element of the array respectively.
- Start swapping arr[i] and arr[j] and keep j fixed and i moving towards j.
- Repeat the above step till i is not equal to j.
Below is the above approach of the above approach:
Python3
# Python3 code for program to
# cyclically rotate an array by one
def rotate(arr, n):
i = 0
j = n - 1
while i != j:
arr[i], arr[j] = arr[j], arr[i]
i=i+1
pass
# Driver function
arr = [1, 2, 3, 4, 5]
n = len(arr)
print("Given array is")
for i in range(0, n):
print(arr[i], end=' ')
rotate(arr, n)
print("\nRotated array is")
for i in range(0, n):
print(arr[i], end=' ')
OutputGiven array is
1 2 3 4 5
Rotated array is
5 1 2 3 4
Time Complexity: O(n), Where n is the number of elements in the array.
Auxiliary Space: O(1), as we are using constant space.
Python Program for Program to cyclically rotate an array by one using Reversal Algorithm:
We can use Reversal Algorithm also , reverse first n-1 elements and then whole array which will result into one right rotation.
Step-by-step approach:
- Reverse the array two times.
- The first time we will reverse the first n-1(n=size of array) elements.
- Finally, we will get our rotated array by reversing the entire array.
Below is the implementation of the above approach:
Python3
arr = [1, 2, 3, 4, 5]
n = len(arr)
k = 1 # No. of rotations
i, j = 0, 0
print("Given array is")
for i in range(n):
print(arr[i], end=" ")
# Reverse the first n-1 terms
for i, j in zip(range(0, (n-k)//2), range(n-k-1, (n-k)//2-1, -1)):
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
# Reverse the entire array
for i, j in zip(range(0, n//2), range(n-1, n//2-1, -1)):
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
print("\nRotated array is")
for i in range(n):
print(arr[i], end=" ")
OutputGiven array is
1 2 3 4 5
Rotated array is
5 1 2 3 4
Time Complexity: O(n), Where n is the number of elements in the array.
Auxiliary Space: O(1), as we are using constant space.|Please refer to the complete article on the Program to cyclically rotate an array by one for more details!
Similar Reads
Program to cyclically rotate an array by one in Python | List Slicing Given an array, cyclically rotate the array clockwise by one. In this article, we will see how to cyclically rotate an array by one in Python. Example Input: arr = [1, 2, 3, 4, 5]Output: arr = [5, 1, 2, 3, 4]Reference: Program to cyclically rotate an array by one Python Program to Cyclically Rotate
2 min read
Python Program for Array Rotation Here we are going to see how we can rotate array with Python code. Array Rotation: Â Python Program for Array Rotation ExamplePartitioning the sub arrays and reversing them Approach: Input arr[] = [1, 2, 3, 4, 5, 6, 7, 8], d = 1, size = 8 1) Reverse the entire list by swapping first and last numbers
7 min read
Python program to right rotate a list by n Given a list and an integer n, write a Python program to right-rotate the list by n position. Examples : Input : n = 2, List_1 = [1, 2, 3, 4, 5, 6]Output : List_1 = [5, 6, 1, 2, 3, 4]Explanation: We get output list after right rotating (clockwise) given list by 2. Input : n = 3, List_1 = [3, 0, 1, 4
6 min read
Python Program for Reversal algorithm for array rotation 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] Rotation of the above array
2 min read
Python Pillow - Flip and Rotate Images Prerequisites: Pillow Python Pillow or PIL is the Python library that provides image editing and manipulating features. The Image Module in it provides a number of functions to flip and rotate images. image.transpose() is the function used to rotate and flip images with necessary keywords as paramet
2 min read
Rotate-matrix module in Python The rotate-matrix is an interesting new python module, which allows conversion of a matrix(2-D array) into either clockwise rotation or anti-clockwise rotation. It at present consists of only two methods. You can loop through these methods for 'n' number of rotations. Installation This module doesn'
1 min read