Skip to content

Instantly share code, notes, and snippets.

@jiankuang
Last active March 17, 2017 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiankuang/147d209dd1c6dfcae6a006e4b99ccabf to your computer and use it in GitHub Desktop.
Save jiankuang/147d209dd1c6dfcae6a006e4b99ccabf to your computer and use it in GitHub Desktop.
Python Data Structures and Algorithms
def array_left_rotation(a, n, k):
if n == k * 2:
for i in range(k):
a[i+k], a[i] = a[i], a[i+k]
else:
temp = a[0]
i = 0
while True:
if i+k < n:
a[i] = a[i+k]
i += k
else:
a[i] = a[i+k-n]
i = i + k - n
if(i==0): break
a[n-k] = temp
return a
n, k = map(int, input().strip().split(' '))
a = list(map(int, input().strip().split(' ')))
answer = array_left_rotation(a, n, k);
print(*answer, sep=' ')
# sample input
# 5 4
# 1 2 3 4 5
# sample output
# 5 1 2 3 4
# sample input
# 20 10
# 41 73 89 7 10 1 59 58 84 77 77 97 58 1 86 58 26 10 86 51
# sample output
# 77 97 58 1 86 58 26 10 86 51 41 73 89 7 10 1 59 58 84 77
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment