Print left rotation of array in O(n) time and O(1) space

Examples :

Input : 
arr[] = {1, 3, 5, 7, 9}
k1 = 1
k2 = 3
k3 = 4
k4 = 6
Output :
3 5 7 9 1
7 9 1 3 5
9 1 3 5 7
3 5 7 9 1
Input :
arr[] = {1, 3, 5, 7, 9}
k1 = 14
Output :
9 1 3 5 7

code:

def first_to_last(arr, num=1):
temp = arr[0]
for i in range(len(arr) - num):
arr[i] = arr[i + 1]
arr[len(arr) - 1] = temp
return arr
def rotate_overall(k):
arr = [1, 3, 5, 7, 9]
for x in range(k):
first_to_last(arr, num=1)
return arr
k1 = 1
print(rotate_overall(k1))
k2 = 3
print(rotate_overall(k2))
k3 = 4
print(rotate_overall(k3))
k4 = 6
print(rotate_overall(k4))
k1= 14
print(rotate_overall(k1))

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sairam Penjarla

Sairam Penjarla

Looking for my next opportunity to make change in a BIG way