We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Just calculate the effective number of rotations d % n and then simply change the list accordingly. Simple Pythonic solution with O(1) complexity:
def rotate(a, r):
l = a[r:] + a[:r]
return l
if __name__ == '__main__':
nd = input().split()
n = int(nd[0])
d = int(nd[1])
a = list(map(int, input().rstrip().split()))
r = d % n
print(*rotate(a,r))
Left Rotation
You are viewing a single comment's thread. Return to all comments →
Just calculate the effective number of rotations d % n and then simply change the list accordingly. Simple Pythonic solution with O(1) complexity: