A left rotation operation on an array of size $n$ shifts each of the array's elements $1$ unit to the left. Given an integer, $\boldsymbol{d}$, rotate the array that many steps left and return the result.  

Example 

$\boldsymbol{d}=2$ 

$arr=[1,2,3,4,5]$  

After $2$ rotations, $ar r'=[3,4,5,1,2]$.

Function Description  

Complete the rotateLeft function in the editor below.  

rotateLeft has the following parameters:  

int d:  the amount to rotate by  
int arr[n]: the array to rotate  

Returns  

int[n]: the rotated array

Input Format

The first line contains two space-separated integers that denote $n$, the number of integers, and $\boldsymbol{d}$, the number of left rotations to perform. 

The second line contains $n$ space-separated integers that describe $\mbox{arr}[]$.  

Constraints

$1\leq n\leq10^5$  
$1\leq d\leq n$  
$1\leq a[i]\leq10^6$

Sample Input
5 4
1 2 3 4 5

Sample Output
5 1 2 3 4

Explanation

To perform $\boldsymbol{d}=4$ left rotations, the array undergoes the following sequence of changes: 

$[1,2,3,4,5]\to[2,3,4,5,1]\to[3,4,5,1,2]\to[4,5,1,2,3]\to[5,1,2,3,4]$

Difficulty: EASY
Tags: Data structures
Skill types: Data structures

Starter code:

Solutions:
(n, d) = map(int, input().split())
arr = [int(x) for x in input().split()]
for i in range(d):
	arr.append(arr[i])
for x in arr[d:]:
	print(x, end=' ')


(n, d) = input().strip().split()
inputList = list(map(int, input().strip().split()))
inputMList = inputList[int(d):] + inputList[:int(d)]
[print(i, end=' ') for i in inputMList]


def read_ints():
	return list(map(int, input().strip().split()))
(n, d) = read_ints()
nums = read_ints()
nums = nums[d % n:] + nums[:d % n]
print(*nums)


from collections import deque
(n, rot) = map(int, input().split())
arr = deque(map(int, input().split()))
arr.rotate(-rot)
print(*list(arr))


(N, d) = map(int, input().split())
A = list(map(int, input().split()))
B = A[d:] + A[:d]
print(' '.join([str(x) for x in B]))


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
for i in range(n):
	t = i + k
	if t > n - 1:
		t %= n
	print(a[t], end=' ')


import sys

def array_rotation(array, n, d):
	m = d
	return array[m:] + array[:m]
line1 = next(sys.stdin)
(n, d) = map(int, line1.strip().split(' '))
array = list(next(sys.stdin).strip().split(' '))
print(' '.join(array_rotation(array=array, n=n, d=d)))


def reverse(l, low, high):
	while low < high:
		(l[low], l[high]) = (l[high], l[low])
		low += 1
		high -= 1
(n, d) = map(int, input().split())
l = list(map(int, input().split()))
reverse(l, 0, d - 1)
reverse(l, d, n - 1)
l.reverse()
for i in range(n):
	print(l[i], end=' ')


(n, d) = map(int, input().split())
a = list(map(int, input().split()))
d %= n
a = a[d:] + a[:d]
print(*a)


from collections import deque
(n, d) = map(int, input().strip().split())
A = deque([int(i) for i in input().strip().split()])
for i in range(d):
	A.append(A.popleft())
print(' '.join(map(str, A)))


(n, d) = map(int, input().split())
a = input().split()
print(*a[d:] + a[:d])


from collections import deque
Line = list(map(int, input().split()))
N = Line[0]
d = Line[1]
Arr = deque(list(map(int, input().split())))
Arr.rotate(-d)
for Item in Arr:
	print(Item, end=' ')


Nd = input()
N = int(Nd.split(' ')[0])
d = int(Nd.split(' ')[1])
A = input().split(' ')
a1 = A[0:d]
a2 = A[d:]
t = a2 + a1
print(' '.join(t))


(_, d) = [int(x) for x in input().strip().split()]
a = [int(x) for x in input().strip().split()]
for _ in range(d):
	x = a.pop(0)
	a.append(x)
for i in a:
	print(i, end=' ')


[n, shift] = [int(num) for num in input().split(' ')]
arr = [int(num) for num in input().split(' ')]
res = arr[shift:] + arr[:shift]
print(' '.join((str(i) for i in res)))


(N, d) = [int(i) for i in input().strip().split()]
A = [int(j) for j in input().strip().split()]
At = []
for i in range(N - d):
	At.append(A[d + i])
for i in range(d):
	At.append(A[i])
A = At
for i in range(N):
	print(A[i], '', end='')


(N, d) = map(int, input().split(' '))
arr = list(map(int, input().split(' ')))
result = arr[d:] + arr[:d]
print(*result, sep=' ')


d = int(input().strip().split()[1])
arr = [int(x) for x in input().strip().split()]
arr = arr[d:] + arr[:d]
print(' '.join((str(x) for x in arr)))


(N, d) = input().split(' ')
(N, d) = (int(N), int(d))
array = list(map(int, input().split(' ')))
for i in range(N):
	print(array[(i + d) % N], end=' ')


(n, d) = input().strip().split()
n = int(n)
d = int(d)
l = [int(x) for x in input().split()]
ll = []
for x in range(d, len(l)):
	ll.append(l[x])
for x in range(d):
	ll.append(l[x])
for x in range(len(ll)):
	print(ll[x], end=' ')


__author__ = 'abdujabbor'
(a, b) = [int(x) for x in input().split()]
ar = [int(x) for x in input().split()]
for x in range(len(ar)):
	v = ar[x + b - len(ar)]
	print(v, end=' ')


(n, d) = input().split()
content = input().split(' ')

def solution(d, content):
	result = content[d:]
	result.extend(content[:d])
	print(' '.join(result))
solution(int(d), content)


(N, d) = map(int, input().split())
a = list(input().split())
r = a[d % N:N] + a[0:d % N]
print(*r, sep=' ')


(n, d) = input().strip().split(' ')
(n, d) = [int(n), int(d)]
A = [int(A_temp) for A_temp in input().strip().split(' ')]
A2 = [0 for i in range(n)]
for k in range(len(A)):
	index = k - d
	A2[index] = A[k]
	A2[index] = str(A2[index])
print(' '.join(A2))


(n, d) = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
end = len(l)
for i in range(d, end):
	print(l[i], end=' ')
for i in range(d):
	print(l[i], end=' ')


(n, d) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
b = a[d:] + a[:d]
for i in b:
	print(i, end=' ')


ll = list(map(int, input().split(' ')))
n = ll[0]
x = ll[1]
lll = list(map(int, input().split(' ')))
shift = x % n
arr = lll[:shift]
lar = lll[shift:]
res = lar + arr
out = ''
for i in range(n):
	if i == n - 1:
		out = out + str(res[i])
	else:
		out = out + str(res[i]) + ' '
print(out)


(N, d) = map(int, input().split())
A = input().split()
rotate = d % N
print(' '.join(A[rotate:] + A[0:rotate]))


(N, d) = map(int, input().strip().split(' '))
array = [int(i) for i in input().strip().split(' ')]

def array_rotate_left(array, d, N):
	output = [None] * N
	for i in range(N):
		output[(i - d) % N] = array[i]
	return output
result = array_rotate_left(array, d, N)
[print(i, end=' ') for i in result]


import sys
arr1 = [int(value) for value in input().strip().split(' ')]
arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]
N = arr1[0]
d = arr1[1]
shift_value = int(d % N)
w = [None] * N
for x in range(0, N):
	q = (x - shift_value) % N
	w[q] = arr[x]
print(' '.join((str(x) for x in w)))


(N, d) = list(map(int, input().split()))
A = list(map(int, input().split()))
s = ''
for j in range(N):
	s += str(A[(j + d) % N]) + ' '
k = len(s)
print(s[0:k - 1])


x = input().split(' ')
rn = x[1]
n = x[0]
nums = input().split(' ')
newnums = nums[int(rn):]
newnums.extend(nums[:int(rn)])
stri = ''
for i in range(0, len(newnums)):
	stri += str(newnums[i])
	stri += ' '
print(stri)


str = input().split(' ')
n = int(str[0])
d = int(str[1])
a = [int(x) for x in input().split(' ')]

def print_arr(arr):
	for i in arr:
		print(i, end=' ')
print_arr(a[d:] + a[:d])


import sys
(n, rot) = [int(i) for i in input().strip().split(' ')]
arr = [int(i) for i in input().strip().split(' ')]
arr = arr[rot:n] + arr[:rot]
s = ''
for i in arr:
	s += str(i) + ' '
print(s.strip())


(_, d) = tuple(map(int, input().split()))
A = list(map(int, input().split()))
[print(i, end=' ') for i in A[d:] + A[:d]]


(N, D) = map(int, input().strip().split(' '))
arr = [int(_) for _ in input().strip().split(' ')]
arr += arr
print(' '.join(map(str, arr[D:D + N])))


(n, d) = (int(x) for x in input().split())
l = [int(x) for x in input().split()]
length = len(l)
new = [0 for x in l]
for i in range(length):
	o = i - d
	new[o] = str(l[i])
print(' '.join(new))


(n, d) = map(int, input().split())
A = list(map(int, input().split()))
print(' '.join(map(str, A[d:] + A[:d])))


(N, d) = [int(i) for i in input().strip().split()]
arr = [int(i) for i in input().strip().split()]

def rotate(items, units):
	return ' '.join((str(e) for e in items[units:] + items[:units]))
print(rotate(arr, d))


import sys
arr_t = [int(arr_temp) for arr_temp in input().strip().split(' ')]
n = arr_t[0]
d = arr_t[1]
arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]
for i in range(0, d):
	arr.append(arr[0])
	arr.pop(0)
for i in arr:
	print(i, end=' ')


(n, rot) = (int(i) for i in input().strip().split(' '))
seq = input().strip().split(' ')
seq = seq[rot:] + seq[:rot]
print(' '.join(seq))


(size, left) = [int(x) for x in input().strip().split()]
arr = [int(x) for x in input().strip().split()]
print(*arr[left:] + arr[:left])


instructions = input().strip().split(' ')
array = input().strip().split(' ')
n = int(instructions[0])
d = int(instructions[1])
final = list()
for i in range(n):
	final.append(array[(i + d) % n])
print(' '.join(final))


def swap(a, i, j):
	tmp = a[i]
	a[i] = a[j]
	a[j] = tmp

def reverse(a, low, high):
	while low < high:
		swap(a, low, high)
		low += 1
		high -= 1

def rotate(a, n, d):
	reverse(a, 0, d - 1)
	reverse(a, d, n - 1)
	reverse(a, 0, n - 1)
	for elem in a:
		print(elem, end=' ')
	print()
(n, d) = [int(temp) for temp in input().strip().split()]
a = [int(temp) for temp in input().strip().split()]
rotate(a, n, d)


import sys
(n, d) = [int(t) for t in input().strip().split(' ')]
v = [int(t) for t in input().strip().split(' ')]
shift = n - d % n
ans = [0] * n
for i in range(0, len(v)):
	ans[(i + shift) % n] = v[i]
print(' '.join([str(t) for t in ans]))


from collections import deque
(n, d) = map(int, input().split())
arr = deque(map(int, input().split()))
arr.rotate(-d)
print(*arr)


(n, k) = input().split(' ')
(n, d) = [int(n), int(k)]
ar = [var + ' ' for var in input().split(' ')]
if d < int(n / 2):
	temp = ar[0:d]
	del ar[0:d]
	st = ''.join(temp)
	ar.append(st)
	print(''.join(ar))
else:
	temp = ar[d:]
	del ar[d:]
	st = ''.join(ar)
	temp.append(st)
	print(''.join(temp))


def rotate(array, N, d):
	rotated = [0] * N
	for (i, el) in enumerate(array):
		rotated[(i - d) % N] = el
	return rotated
(N, d) = (int(s) for s in input().split())
array = input().split()
print(' '.join(rotate(array, N, d)))


(n, d) = map(int, input().split())
array = list(map(int, input().split()))
j = 0
k = 0
temp = 0

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
for i in range(gcd(n, d)):
	temp = array[i]
	j = i
	while True:
		k = j + d
		if k >= n:
			k -= n
		if k == i:
			break
		array[j] = array[k]
		j = k
	array[j] = temp
for i in range(n):
	if i != n - 1:
		print(array[i], end=' ')
	else:
		print(array[i])


import collections
arr_nd = list(map(int, input().strip().split()))
n = arr_nd[0]
d = arr_nd[1]
arr_vals = input().strip().split()
dk_vals = collections.deque(arr_vals)
dk_vals.rotate(-1 * d)
print(' '.join(map(str, dk_vals)))


(n, d) = map(int, input().strip().split())
arr = input().strip().split()
for i in range(n - d):
	pass
	arr.insert(0, arr.pop())
print(' '.join(arr))


(n, d) = list(map(int, input().strip().split(' ')))
arr = list(map(int, input().strip().split(' ')))
print(' '.join((str(x) for x in arr[d:] + arr[:d])))


(_, rotations) = map(int, input().strip().split(' '))
array = list(map(int, input().strip().split(' ')))
print(' '.join([str(array[(i + rotations) % len(array)]) for i in range(len(array))]))


def read_ints():
	return list(map(int, input().split()))

def rotate(l, n):
	return l[n:] + l[:n]
(n, d) = read_ints()
arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]
ans = rotate(arr, d)
for i in ans:
	print(i, end=' ')
print('')


(b, a) = (int(arr_temp) for arr_temp in input().strip().split(' '))
c = [int(arr_temp) for arr_temp in input().strip().split(' ')]
for i in range(b):
	print(c[(a + i) % b], end=' ')


info = input().split(' ')
(n, d) = (int(info[0]), int(info[1]))
arr = input().split(' ')
print(' '.join([arr[(i + d) % n] for i in range(n)]))


def read_ints():
	return list(map(int, input().split()))

def shiftInPlace(l, n):
	n = n % len(l)
	head = l[:n]
	l[:n] = []
	l.extend(head)
	return l
[array_size, rotation_length] = read_ints()
current_list = list(map(int, input().split()))
new_list = shiftInPlace(current_list, rotation_length)
print(*new_list, sep=' ')


(n, nr) = input().split()
inList = input().split()
temp = -(int(n) - int(nr))
for x in range(int(n)):
	print(inList[temp], end=' ')
	temp = temp + 1


(n, d) = [int(arr_temp) for arr_temp in input().strip().split(' ')]
arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]
for i in range(d):
	temp = arr.pop(0)
	arr.append(temp)
output = ''
for i in arr:
	output += str(i) + ' '
print(output)


(n, r) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
for each in range(r):
	a.append(a.pop(0))
print(' '.join(map(str, a)))


import sys

def main():
	form = [int(i) for i in input().split()]
	arr = [int(i) for i in input().split()]
	doRotation(arr, form[1])

def doRotation(arr, d):
	temp_arr = [0 for i in range(len(arr))]
	for i in range(len(arr)):
		num_to_shift = arr[i]
		new_index = i - d
		if new_index < 0:
			new_index = len(arr) - abs(new_index)
		temp_arr[new_index] = num_to_shift
	for num in range(len(temp_arr)):
		print('{} '.format(temp_arr[num]), end='')
main()


(n, d) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = a[d:] + a[:d]
for x in b:
	print(x, end=' ')
print()


(n, num_rotations) = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]

def left_rotate(array, num_rotations):
	if num_rotations == len(array) or num_rotations % len(array) == 0:
		return array
	elif num_rotations < len(array):
		return array[num_rotations:] + array[:num_rotations]
	else:
		num_rotations = num_rotations % len(array)
		return array[num_rotations:] + array[:num_rotations]
print(*left_rotate(arr, num_rotations))


def leftShift(sample, d):
	slen = len(sample)
	output = [None] * slen
	for i in range(0, slen):
		shiftIndex = (i - d + slen) % slen
		output[shiftIndex] = sample[i]
	return output
metadata = [int(x) for x in input().split(' ')]
sample = [int(x) for x in input().split(' ')]
sample = leftShift(sample, metadata[1])
print(' '.join(map(str, sample)))


(n, d) = [int(x) for x in input().split()]
l = [x for x in input().split()]

def shift_left_by_n(l, n):
	ll = []
	for i in range(len(l)):
		ll.append(l[(i + n) % len(l)])
	return ll
print(' '.join(shift_left_by_n(l, d)))


(n, r) = map(int, input().split(' '))
arr = input().split(' ')
if r > len(arr):
	r = r % len(arr)
op = arr[r:]
op.extend(arr[:r])
print(' '.join(op))


(n, r) = map(int, input().split(' '))
arr = input().split(' ')
if r > len(arr):
	r = r % len(arr)
op = arr[r:]
op.extend(arr[:r])
print(' '.join(op))


(n, d) = map(int, input().strip().split())
arr = list(map(int, input().strip().split()))
arr = arr[d:] + arr[0:d]
print(' '.join((str(i) for i in arr)))


def mover(l, left):
	left = int(left)
	ll = l + ' ' + l
	ll = ll.split(' ')
	move = len(ll) // 2
	ret = ll[left:left + move]
	return ' '.join(ret)
left = input().split(' ')[1]
l = input()
print(mover(l, left))


linea = input()
n = int(linea.split(' ')[0])
veces = int(linea.split(' ')[1])
vector = []
valores = input()
vector = valores.split(' ')
for x in range(veces):
	vector.append(vector[0])
	vector.pop(0)
s = ' '
print(s.join(vector))


(_, r) = map(int, input().split())
a = list(map(int, input().split()))
for i in range(r, r + len(a)):
	print(a[i % len(a)], end=' ')
print()


def left_rotate(ar, d):
	N = len(ar)
	d = d % N
	return ar[d:] + ar[:d]
(n, d) = map(int, input().split())
ar = list(map(int, input().split()))
ar = left_rotate(ar, d)
print(' '.join([str(x) for x in ar]))


def left_shift(arr, offset):
	return arr[offset:] + arr[:offset]
d = input()
n = input()
d = [int(i) for i in d.split()]
n = [int(i) for i in n.split()]
print(' '.join((str(x) for x in left_shift(n, d[1]))))


(n, k) = map(int, input().strip().split(' '))
arr = list(map(int, input().strip().split(' ')))
temp = arr[:k]
arr = arr[k:]
for i in temp:
	arr.append(i)
print(*arr, sep=' ')


(n, d) = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
x = d % n
r = a[x:]
r.extend(a[:x])
print(*r)


(length, rotations) = map(int, input().strip().split())
l = list(map(int, input().strip().split()))
rotations = rotations % length
for i in range(rotations, length):
	print(l[i], end=' ')
for i in range(0, rotations):
	print(l[i], end=' ')


S = input().strip().split()
n = int(S[0])
d = int(S[1])
tmp = list(map(int, input().strip().split()))
arr = []
for i in range(n):
	if d == n:
		d = 0
	arr.append(tmp[d])
	d += 1
print(*arr)


import sys
header = sys.stdin.readline().strip().split()
n = int(header[0])
d = int(header[1])
arr = sys.stdin.readline().strip().split()
rotated_arr = arr[d:n] + arr[0:d]
print(' '.join(rotated_arr))


(length, rnum) = map(int, input().split())
arr = input().split()
print(' '.join(arr[rnum % length:] + arr[:rnum % length]))


(n, d) = input().strip().split(' ')
d = int(d)
array = [int(x) for x in input().split(' ')]
new_arr = array[d:]
for x in array[0:d]:
	new_arr.append(int(x))
fin = ' '.join(map(str, new_arr))
print(fin)


(n, d) = map(int, input().split())
a = list(input().split())
print(' '.join(a[d:] + a[:d]))


import sys
args = input().strip().split(' ')
n = int(args[0])
d = int(args[1])
arr = input().strip().split(' ')
out = []
for i in range(d, len(arr)):
	out.append(arr[i])
for i in range(0, d):
	out.append(arr[i])
print(' '.join(out))


(n, d) = [int(x) for x in input().strip().split(' ')]
arr = [int(x) for x in input().strip().split(' ')]
c = n
while n > 0:
	print(arr[d % c], end=' ')
	d += 1
	n -= 1


inputs = list(map(int, input().split(' ')))
my_array = list(map(int, input().split(' ')))
moved_array = my_array[inputs[1]:] + my_array[:inputs[1]]
print_str = ''
for i in range(inputs[0]):
	if i < inputs[0] - 1:
		print_str += str(moved_array[i]) + ' '
	else:
		print_str += str(moved_array[i])
print(print_str)


def lrotate(a, d):
	return a[d % len(a):] + a[0:d % len(a)]

def lrotate2(a, d):
	a = a[:]
	for x in range(d):
		f = a[0]
		a = a[1:]
		a.append(f)
	return a
(n, d) = list(map(int, input().strip().split(' ')))
a = list(map(int, input().strip().split(' ')))
assert n == len(a)
print(' '.join(map(str, lrotate(a, d))))


def solve(A, d):
	idx = d % len(A)
	return A[idx:] + A[:idx]
(n, d) = list(map(int, input().split()))
A = list(map(int, input().split()))
print(*solve(A, d))


(num, rot) = map(int, input().split())
arr = input().split()
print(' '.join(arr[rot % num:] + arr[:rot % num]))


from sys import stdin
raw = [line.strip() for line in stdin.readlines()]
left_moves = int(raw[0].split()[1])
arr = [int(n) for n in raw[1].split()]
starting_index = (0 + left_moves) % len(arr)
result = ''
for i in range(len(arr)):
	index = (starting_index + i) % len(arr)
	result += str(arr[index]) + ' '
print(result.strip())


(n, k) = [int(i) for i in input().strip().split(' ')]
li = [int(i) for i in input().strip().split(' ')]
d = k % n
ul = li[d:] + li[0:d]
for i in ul:
	print(i, end=' ')


ini = [int(y) for y in input().strip().split(' ')]
arr = [int(x) for x in input().strip().split(' ')]
n = ini[0]
d = ini[1]
index = d % n
arr = arr[index:] + arr[:index]
print(*arr)


(l, n) = map(int, input().split())
arr = input().split()
print(' '.join(arr[n:] + arr[:n]))


(n, d) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
for i in range(len(a)):
	print(a[(d + i) % n], end=' ')


(n, d) = input().split(' ')
(n, d) = (int(n), int(d))

def rotate(l, n):
	return l[n:] + l[:n]
a = [int(x) for x in input().split(' ')]
a = rotate(a, d)
for i in a:
	print(i, end=' ')


(n, d) = map(int, input().split(' '))
original_array = input().split(' ')
rotated_array = original_array[d:] + original_array[:d]
print(' '.join(rotated_array))


def rotate(L, d):
	n = len(L)
	for i in range(n):
		print(L[(i + d) % n], end=' ')
(n, d) = [int(x) for x in input().strip().split()]
L = [int(x) for x in input().strip().split()]
rotate(L, d)


(n, d) = map(int, input().split())
d %= n
nums = input().split()
print(' '.join(nums[d:] + nums[:d]))


t = input().strip().split()
n = int(t[0])
d = int(t[1])
a = input().strip().split()
for rotation in range(d):
	a.append(a.pop(0))
print(' '.join(a))


(n, d) = map(int, input().split())
num_list = list(map(int, input().split()))

def make_sorted_tuplist(your_list, n, d):
	tup_list = []
	for i in range(len(your_list)):
		index = i - d
		if index < 0:
			index += n
		tup_list.append((your_list[i], index))
	return sorted(tup_list, key=lambda x: x[1])

def rotated_list(tup_list):
	return [str(item[0]) for item in tup_list]
print(' '.join(rotated_list(make_sorted_tuplist(num_list, n, d))))