Given an array of size N. The task is to rotate array by D elements where D ≤ N.
Example 1:
Input:
N = 7
Arr[] = {1, 2, 3, 4, 5, 6, 7}
D = 2
Output: 3 4 5 6 7 1 2
Explanation: 
Rotate by 1: [2, 3, 4, 5, 6, 7, 1]
Rotate by 2: [3, 4, 5, 6, 7, 1, 2]
Example 2:
Input:
N = 4
Arr[] = {1, 3, 4, 2}
D = 3
Output: 2 1 3 4
Your Task:
You don't need to read input or print anything. Your task is to complete the function leftRotate() which takes the array of integers arr[], its size n and d as input parameters and rotates arr[] in-place without using any extra memory.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ Arr[i] ≤ 1000
0 ≤ D ≤ N

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



class Solution:

    def leftRotate(self, arr, n, d):

        # code here

Solutions:
class Solution:

	def leftRotate(self, arr, n, d):
		arr[:] = arr[d:] + arr[:d]
		return arr


class Solution:

	def reverse(self, arr, left, right):
		while left <= right:
			(arr[left], arr[right]) = (arr[right], arr[left])
			left += 1
			right -= 1

	def leftRotate(self, arr, n, d):
		if n == 0:
			return
		d = d % n
		if d == 0:
			return
		self.reverse(arr, 0, d - 1)
		self.reverse(arr, d, n - 1)
		self.reverse(arr, 0, n - 1)


class Solution:

	def leftRotate(self, arr, n, d):
		d = d % n
		if d == 0:
			return
		temp = []
		for i in range(d):
			temp.append(arr[i])
		for i in range(d, n):
			arr[i - d] = arr[i]
		for i in range(n - d, n):
			arr[i] = temp[i - (n - d)]


class Solution:

	def leftRotate(self, arr, n, d):

		def reverse(arr, first, last):
			while first < last:
				(arr[first], arr[last]) = (arr[last], arr[first])
				first += 1
				last -= 1
		if d == 0 or d == n:
			return arr
		else:
			reverse(arr, 0, d - 1)
			reverse(arr, d, n - 1)
			reverse(arr, 0, n - 1)
			return arr


class Solution:

	def leftRotate(self, arr, n, d):
		d = d % n
		l = arr[d:]
		l1 = arr[:d]
		j = 0
		for i in l:
			arr[j] = i
			j += 1
		for i in l1:
			arr[j] = i
			j += 1
		return arr


class Solution:

	def leftRotate(self, arr, n, d):

		def reverse(arr, start, end):
			while start <= end:
				(arr[start], arr[end]) = (arr[end], arr[start])
				start += 1
				end -= 1
		d = d % n
		if d == 0:
			return arr
		else:
			reverse(arr, 0, d - 1)
			reverse(arr, d, n - 1)
			reverse(arr, 0, n - 1)


class Solution:

	def leftRotate(self, arr, n, d):
		lists = [arr[(i + d) % n] for i in range(0, n)]
		for i in range(0, len(lists)):
			arr[i] = lists[i]


class Solution:

	def leftRotate(self, arr, n, d):
		d = d % len(arr)
		if d > 0:
			arr[:] = arr[d:] + arr[:d]


class Solution:

	def leftRotate(self, arr, n, d):
		d = d % n
		l = 0
		r = n - 1
		while l <= r:
			(arr[l], arr[r]) = (arr[r], arr[l])
			l += 1
			r -= 1
		l = n - d
		r = n - 1
		while l <= r:
			(arr[l], arr[r]) = (arr[r], arr[l])
			l += 1
			r -= 1
		l = 0
		r = n - 1 - d
		while l <= r:
			(arr[l], arr[r]) = (arr[r], arr[l])
			l += 1
			r -= 1
		return arr


class Solution:

	def leftRotate(self, arr, n, k):
		k = k % n
		(i, m) = (0, k - 1)
		while i < m:
			temp = arr[i]
			arr[i] = arr[m]
			arr[m] = temp
			i = i + 1
			m = m - 1
		x = n - 1
		while k < x:
			temp = arr[k]
			arr[k] = arr[x]
			arr[x] = temp
			k = k + 1
			x = x - 1
		i = 0
		j = n - 1
		while i < j:
			temp = arr[i]
			arr[i] = arr[j]
			arr[j] = temp
			i = i + 1
			j = j - 1
		return arr


class Solution:

	def rev(self, arr, i, j):
		if i > j:
			(i, j) = (j, i)
		new_arr = arr[i:j + 1]
		new_arr.reverse()
		arr[i:j + 1] = new_arr

	def leftRotate(self, arr, n, d):
		if d > n:
			d = d % n
		arr[:] = arr[::-1]
		self.rev(arr, 0, n - d - 1)
		self.rev(arr, n - d, n)
		return arr


class Solution:

	def leftRotate(self, arr, n, d):
		k = d
		if k > n:
			k = k % n
		ans = []
		for x in arr[k:]:
			ans.append(x)
		for x in arr[:k]:
			ans.append(x)
		i = 0
		while i < n:
			arr[i] = ans[i]
			i += 1
		return ans


class Solution:

	def leftRotate(self, arr, n, k):
		i = 0
		j = n - 1
		k = k % n
		while i < j:
			(arr[i], arr[j]) = (arr[j], arr[i])
			i += 1
			j -= 1
		i = 0
		j = n - k - 1
		while i < j:
			(arr[i], arr[j]) = (arr[j], arr[i])
			i += 1
			j -= 1
		i = n - k
		j = n - 1
		while i < j:
			(arr[i], arr[j]) = (arr[j], arr[i])
			i += 1
			j -= 1
		return arr


class Solution:

	def leftRotate(self, arr, n, d):
		arr_temp = arr[0:d]
		for i in range(d):
			arr.pop(0)
		arr.extend(arr_temp)


class Solution:

	def leftRotate(self, arr, n, d):
		d = d % n
		self.reverse(0, d - 1, arr)
		self.reverse(d, n - 1, arr)
		self.reverse(0, n - 1, arr)

	def reverse(self, low, high, arr):
		while low < high:
			temp = arr[low]
			arr[low] = arr[high]
			arr[high] = temp
			low += 1
			high -= 1


class Solution:

	def leftRotate(self, arr, n, d):
		d = d % n
		arr[:] = arr[d:n] + arr[0:d]


class Solution:

	def leftRotate(self, arr, n, d):
		d = d % n
		for i in range(d):
			arr.append(arr.pop(0))


from collections import deque

class Solution:

	def reverse(self, arr, b, e):
		while b < e:
			(arr[b], arr[e]) = (arr[e], arr[b])
			b += 1
			e -= 1

	def leftRotate(self, arr, n, d):
		if d == 0:
			return arr
		self.reverse(arr, 0, d - 1)
		self.reverse(arr, d, n - 1)
		self.reverse(arr, 0, n - 1)


class Solution:

	def reverse(self, array, i, j):
		if i > j:
			(i, j) = (j, i)
		section = array[i:j + 1]
		section.reverse()
		array[i:j + 1] = section

	def leftRotate(self, arr, n, k):
		if k > n:
			k = k % n
		arr[0:n] = arr[::-1]
		self.reverse(arr, 0, n - k - 1)
		self.reverse(arr, n - k, n)


class Solution:

	def leftRotate(self, arr, n, d):
		if d % n == 0:
			return arr
		self.swap(arr, 0, d - 1)
		self.swap(arr, d, n - 1)
		self.swap(arr, 0, n - 1)
		return arr

	def swap(self, arr, i, j):
		while i < j:
			(arr[i], arr[j]) = (arr[j], arr[i])
			i += 1
			j -= 1


class Solution:

	def reverse(self, arr, start, end):
		while start < end:
			temp = arr[start]
			arr[start] = arr[end]
			arr[end] = temp
			start += 1
			end -= 1

	def leftRotate(self, arr, n, d):
		d = d % n
		if d < 0:
			d = d + n
		self.reverse(arr, 0, d - 1)
		self.reverse(arr, d, n - 1)
		self.reverse(arr, 0, n - 1)


class Solution:

	def leftRotate(self, arr, n, d):
		if d > n:
			d = d % n
		arr[:] = arr[d:] + arr[:d]


class Solution:

	def leftRotate(self, arr, n, d):
		d %= n
		(left, right) = (0, d - 1)
		while left < right:
			(arr[left], arr[right]) = (arr[right], arr[left])
			left += 1
			right -= 1
		(left, right) = (d, n - 1)
		while left < right:
			(arr[left], arr[right]) = (arr[right], arr[left])
			left += 1
			right -= 1
		(left, right) = (0, n - 1)
		while left < right:
			(arr[left], arr[right]) = (arr[right], arr[left])
			left += 1
			right -= 1


class Solution:

	def leftRotate(self, arr, n, d):
		k = d % n

		def swap(arr, l, e):
			while l < e:
				(arr[l], arr[e]) = (arr[e], arr[l])
				l += 1
				e -= 1
		swap(arr, 0, k - 1)
		swap(arr, k, n - 1)
		swap(arr, 0, n - 1)


class Solution:

	def leftRotate(self, arr, n, d):
		j = 0
		d = d % n
		temp = arr[:d]
		for i in range(d, n):
			arr[j] = arr[i]
			j += 1
		for i in range(len(temp)):
			arr[j] = temp[i]
			j += 1
		return arr


class Solution:

	def leftRotate(self, arr, n, d):
		x = arr[d:] + arr[:d]
		for i in range(0, n):
			arr[i] = x[i]
		return x


class Solution:

	def leftRotate(self, arr, n, d):
		temp = []
		for i in range(d):
			temp.append(arr[i])
		for i in range(n - d):
			(arr[i], arr[i + d]) = (arr[i + d], arr[i])
		for j in range(n - d, n):
			arr[j] = temp[j - n + d]


class Solution:

	def reverse(self, arr, i, j):
		while i < j:
			temp = arr[i]
			arr[i] = arr[j]
			arr[j] = temp
			i += 1
			j -= 1

	def leftRotate(self, arr, n, d):
		d = d % n
		self.reverse(arr, 0, d - 1)
		self.reverse(arr, d, n - 1)
		self.reverse(arr, 0, n - 1)


class Solution:

	def leftRotate(self, arr, n, d):
		a = arr[:d]
		b = arr[d:]
		arr[:] = b + a


class Solution:

	def reverse(self, nums, s, e):
		while s < e:
			(nums[s], nums[e]) = (nums[e], nums[s])
			s += 1
			e -= 1

	def leftRotate(self, nums, n, k):
		k = k % n
		self.reverse(nums, 0, n - 1)
		self.reverse(nums, 0, n - k - 1)
		self.reverse(nums, n - k, n - 1)


class Solution:

	def leftRotate(self, arr, n, d):
		if d == n:
			return arr
		if d > n:
			d = d % n
		arr[0:d] = reversed(arr[0:d])
		arr[d:n] = reversed(arr[d:n])
		arr.reverse()
		return arr


class Solution:

	def leftRotate(self, arr, n, d):
		idx = 0
		if d == n:
			return arr
		while d > 0:
			val = arr.pop(idx)
			arr.append(val)
			d -= 1
		return arr


class Solution:

	def leftRotate(self, arr, n, d):
		ans = [None] * len(arr)
		for i in range(len(arr)):
			ans[(i - d) % n] = arr[i]
		for i in range(n):
			arr[i] = ans[i]


class Solution:

	def leftRotate(self, arr, n, d):
		brr = []
		arr[0:n:1] = arr[d:n] + arr[0:d]
		return arr


class Solution:

	def leftRotate(self, arr, n, d):
		d = d % len(arr)
		arr[:] = arr[d:] + arr[:d]


class Solution:

	def leftRotate(self, a, n, d):
		a.reverse()
		a[0:n - d] = list(reversed(a[0:n - d]))
		a[n - d:n] = list(reversed(a[n - d:n]))


class Solution:

	def leftRotate(self, arr, n, d):
		a = arr[:d]
		del arr[:d]
		arr.extend(a)
		return arr
		a = arr[:d]
		del arr[:d]
		for i in a:
			arr.append(i)
		return arr


class Solution:

	def leftRotate(self, arr, n, d):
		tmp = arr[:d]
		for i in range(n - d):
			arr[i] = arr[i + d]
		for i in range(len(tmp)):
			arr[n + i - d] = tmp[i]


class Solution:

	def reverse(self, nums, s, e):
		i = s
		while i < e:
			(nums[i], nums[e - 1]) = (nums[e - 1], nums[i])
			i = i + 1
			e = e - 1

	def leftRotate(self, nums, n, d):
		n = len(nums)
		d = d % n
		d = n - d
		self.reverse(nums, 0, n)
		self.reverse(nums, 0, d)
		self.reverse(nums, d, n)


class Solution:

	def leftRotate(self, arr, n, d):
		a = arr[:d]
		del arr[:d]
		arr.extend(a)
		return arr


class Solution:

	def leftRotate(self, arr, n, d):
		copyArr = [0] * n
		for i in range(n):
			newI = (i - d) % n
			copyArr[newI] = arr[i]
		for i in range(n):
			arr[i] = copyArr[i]


class Solution:

	def leftRotate(self, arr, n, d):
		temp = []
		for m in range(d):
			temp.append(arr[m])
		for i in range(d, n):
			arr[i - d] = arr[i]
		j = 0
		for l in range(n - d, n):
			arr[l] = temp[j]
			j += 1
		return arr


class Solution(object):

	def leftRotate(self, arr, n, k):

		def reverse(left, right):
			while left < right:
				(arr[left], arr[right]) = (arr[right], arr[left])
				left += 1
				right -= 1
		k %= n
		size = n - 1
		reverse(0, size)
		reverse(0, size - k)
		reverse(n - k, size)
		return arr


class Solution:

	def leftRotate(self, arr, n, d):
		d = d % n
		for i in range(0, d, 1):
			j = 0
			arr.append(arr[j])
			arr[j] = 0
			arr.remove(0)
		return arr


class Solution:

	def leftRotate(self, a, n, k):

		def rotate(s, e, a):
			(i, j) = (s, e)
			while i < j:
				(a[i], a[j]) = (a[j], a[i])
				i += 1
				j -= 1
		rotate(0, k - 1, a)
		rotate(k, n - 1, a)
		rotate(0, n - 1, a)
		return a


class Solution:

	def leftRotate(self, a, n, k):
		temp = []
		for i in range(k, n):
			temp.append(a[i])
		for i in range(0, k):
			temp.append(a[i])
		a[:] = temp
		return a


class Solution:

	def leftRotate(self, arr, n, d):
		d = d % n
		k = d

		def reverseArray(arr, start, end):
			while start < end:
				temp = arr[start]
				arr[start] = arr[end]
				arr[end] = temp
				start += 1
				end -= 1
			return arr
		k = k % n
		arr = reverseArray(arr, 0, k - 1)
		arr = reverseArray(arr, k, len(arr) - 1)
		arr = reverseArray(arr, 0, len(arr) - 1)
		return arr


class Solution:

	def leftRotate(self, arr, n, k):
		k = k % n
		if k == n or k == 0:
			return arr
		ans = [-1] * n
		j = n - 1
		for i in range(k - 1, -1, -1):
			ans[j] = arr[i]
			j -= 1
		for i in range(n - 1, k - 1, -1):
			ans[j] = arr[i]
			j -= 1
		for i in range(n):
			arr[i] = ans[i]


class Solution:

	def leftRotate(self, arr, n, d):
		if d == n or n == 1:
			return arr
		for i in range(d):
			ele = arr[0]
			arr.remove(ele)
			arr.append(ele)
		return arr


class Solution:

	def gcd(self, a, b):
		return a if b == 0 else self.gcd(b, a % b)

	def leftRotate(self, arr, n, d):
		d %= n
		g = self.gcd(n, d)
		for i in range(g):
			t = arr[i]
			j = i
			for _ in range(n // g - 1):
				arr[j] = arr[(j + d) % n]
				j = (j + d) % n
			arr[j] = t


class Solution:

	def leftRotate(self, arr, n, k):
		if k == 0 or (k >= n and k % n == 0):
			return
		popped = 0
		index = 0
		rotation_count = k % n
		while rotation_count > 0:
			popped = arr.pop(index)
			arr.append(popped)
			rotation_count = rotation_count - 1


class Solution:

	def leftRotate(self, arr, n, d):
		a = arr[0:d]
		del arr[0:d]
		arr.extend(a)
		del a