Python Program for Program to cyclically rotate an array by one
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}
Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.
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[ ] = {1, 2, 3, 4, 4}
- arr[3] = arr[2]
- arr[ ] = {1, 2, 3, 3, 4}
- arr[2] = arr[1]
- arr[ ] = {1, 2, 2, 3, 4}
- arr[1] = arr[0]
- arr[ ] = {1, 1, 2, 3, 4}
- Assign arr[0] = last_el
- arr[0] = 5
- arr[ ] = {5, 1, 2, 3, 4}
- 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 |
Given 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 = ' ' ) |
Given 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 = " " ) |
Given 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!