Find element at given index after a number of rotations

Examples :

Input : arr[] : {1, 2, 3, 4, 5}
ranges[] = { {0, 2}, {0, 3} }
index : 1
Output : 3
Explanation : After first given rotation {0, 2}
arr[] = {3, 1, 2, 4, 5}
After second rotation {0, 3}
arr[] = {4, 3, 1, 2, 5}
After all rotations we have element 3 at given
index 1.

code:

arr = [1, 2, 3, 4, 5]
ranges = [[0,2],[0,3]]
rotate_by = 2
def something(arr,index,rotate_by,ranges):
for i in ranges:
left = 0
right = i[1]+1
index = (index-rotate_by)%len(arr[left:right])
#print('index is pointing at ', arr[index])
return arr[index]

print(something(arr,ranges[0][1],rotate_by,ranges))





def get_index(arr,position):
position = ((position - 2) % len(arr))
return arr[pos]

--

--

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