Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Python Program for Program to cyclically rotate an array by one

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Given an array, cyclically rotate the array clockwise by one.

Examples:

Input:  arr[] = {1, 2, 3, 4, 5}
Output: arr[] = {5, 1, 2, 3, 4}

Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Python3




# Python3 code for program to 
# cyclically rotate an array by one
  
# Method for rotation
def rotate(arr, n):
    x = arr[n - 1]
      
    for i in range(n - 1, 0, -1):
        arr[i] = arr[i - 1];
          
    arr[0] = x;
  
  
# 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

Please refer complete article on Program to cyclically rotate an array by one for more details!

My Personal Notes arrow_drop_up
Last Updated : 26 Dec, 2021
Like Article
Save Article
Similar Reads