Given an array arr[] of size N and an integer K, the task is to left rotate the array K indexes
Example 1:
Input: N = 7, K = 2
arr[] = {1, 2, 3, 4, 5, 6, 7}
Output: 3 4 5 6 7 1 2
Explanation: Rotation of the above 
array by 2 will make the output array .
Example 2: 
Input: N = 6, K = 12
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 2 3 4 5 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function leftRotate() that takes array arr, integer K and integer N as parameters and rotate the given array by k value. You have to rotate array even the k is greater than n. In these case after every n rotation array comes same as the original array.
 
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
 
Constraints:
1 ≤ N ≤ 10^{5
}1 ≤ K ≤ 10^{5
}-100 ≤ arr[i] ≤ 100

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: class Solution:

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

        # Your code goes here

Solutions:
class Solution:

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

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


class Solution:

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

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


class Solution:

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


class Solution:

	def leftRotate(self, arr, k, n):
		if k > n:
			k = k % n
		if k > 0:
			temp = arr[:k]
			for i in range(k, n):
				arr[i - k] = arr[i]
			for i in range(len(temp)):
				arr[n - k + i] = temp[i]


class Solution:

	def leftRotate(self, arr, k, n):
		if k > n:
			d = k % n
		elif k <= n:
			d = k

		def reverse(arr, first, last):
			while first < last:
				(arr[first], arr[last]) = (arr[last], arr[first])
				first += 1
				last -= 1
		if d == 0:
			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, k, n):

		def gcd(a, b):
			if b == 0:
				return a
			return gcd(b, a % b)
		k = k % n
		p = gcd(k, n)
		if k == 0:
			return
		for i in range(p):
			tmp = arr[i]
			j = i
			while 1:
				c = j + k
				c %= n
				if c == i:
					break
				arr[j] = arr[c]
				j = c
			arr[j] = tmp


class Solution:

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


class Solution:

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


class Solution:

	def leftRotate(self, arr, d, n):
		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, k, n):
		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 reverseArray(self, arr, start, end):
		while start < end:
			temp = arr[start]
			arr[start] = arr[end]
			arr[end] = temp
			start += 1
			end = end - 1

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


class Solution:

	def leftRotate(self, arr, k, n):
		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, k, n):
		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, k, n):
		a = []
		if k > n:
			k = k % n
		for i in range(k):
			a.append(arr[i])
		for i in range(k, n):
			arr[i - k] = arr[i]
		z = 0
		for i in range(n - k, n):
			arr[i] = a[z]
			z += 1
		return arr


class Solution:

	def leftRotate(self, arr, k, n):
		if k % n:
			k = k % n
			helper = arr[:k + 1]
			for i in range(0, n - k):
				arr[i] = arr[i + k]
			for i in range(0, k):
				arr[i + n - k] = helper[i]


class Solution:

	def leftRotate(self, arr, k, n):
		if k > n:
			k = n - k % n
			b = arr[n - k:n] + arr[0:n - k + 1]
			arr[0:n] = b[0:n]
			return arr
		b = arr[k:n] + arr[0:k]
		arr[0:n] = b[0:n]
		return


class Solution:

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


class Solution:

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

	def leftRotate(self, A, d, N):
		while d > N:
			d = d - N
		self.reverse(A, 0, d - 1)
		self.reverse(A, d, N - 1)
		self.reverse(A, 0, N - 1)


class Solution:

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

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


class Solution:

	def leftRotate(self, arr, k, n):
		if k > n:
			k = k % n
		a = []
		for i in range(k, n):
			a.append(arr[i])
		for i in range(0, k):
			a.append(arr[i])
		for i in range(0, n):
			arr[i] = a[i]


class Solution:

	def leftRotate(self, arr, k, n):
		l = []
		if n > k:
			r = k
		else:
			r = k % n
		for i in range(r, n):
			l.append(arr[i])
		for i in range(0, r):
			l.append(arr[i])
		for i in range(0, n):
			arr[i] = l[i]


class Solution:

	def leftRotate(self, arr, k, n):
		k = k % n
		dupli = arr[:k]
		for i in range(k, n):
			arr[i - k] = arr[i]
		for i in range(n - k, n):
			arr[i] = dupli[i - n + k]
		return arr


class Solution:

	def leftRotate(self, arr, k, n):
		k = k % 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, k, n):
		k = k % n
		t = arr[k:] + arr[:k]
		for i in range(n):
			arr[i] = t[i]


class Solution:

	def leftRotate(self, arr, k, n):
		k = k % n
		if k == 0:
			return arr
		count = 0
		for i in range(n):
			src_index = i
			src_elem = arr[i]
			while True:
				dest_index = src_index - k
				if dest_index < 0:
					dest_index += n
				temp = arr[dest_index]
				arr[dest_index] = src_elem
				src_elem = temp
				src_index = dest_index
				count += 1
				if dest_index == i:
					break
			if count == n:
				return arr
		return arr


class Solution:

	def leftRotate(self, arr, k, n):
		j = 0
		k = k % n
		temp = arr[:k]
		for i in range(k, 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, k, n):
		k = k % n
		arr[0:k] = reversed(arr[0:k])
		arr[k:n] = reversed(arr[k:n])
		arr[0:n] = reversed(arr[0:n])


class Solution:

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

		def solve(l, h):
			while l < h:
				(arr[l], arr[h]) = (arr[h], arr[l])
				l += 1
				h -= 1
		k = k % n
		solve(0, n - 1)
		solve(0, n - k - 1)
		solve(n - k, n - 1)


class Solution:

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


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, k, n):
		k = k % n
		self.reverse(arr, 0, k - 1)
		self.reverse(arr, k, n - 1)
		self.reverse(arr, 0, n - 1)


class Solution:

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

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


class Solution:

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

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


class Solution:

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


class Solution:

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


class Solution:

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


class Solution:

	def leftRotate(self, arr, k, n):
		start = k % n
		j = 0
		x = arr[:start]
		for i in range(start, n):
			arr[j] = arr[i]
			j = j + 1
		z = 0
		for i in range(j, n):
			arr[i] = x[z]
			z = z + 1


class Solution:

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


class Solution:

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


class Solution:

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


class Solution:

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


class Solution:

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


class Solution(object):

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

		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, k, n):
		k = k % n
		if k == 0 or k == n:
			return arr
		self.swap(arr, 0, k - 1)
		self.swap(arr, k, n - 1)
		self.swap(arr, 0, n - 1)

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


class Solution:

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

		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, k, n):
		k = k % n
		m = []
		for i in range(k):
			m.append(arr[i])
		for i in range(k, n):
			arr[i - k] = arr[i]
		for i in range(k):
			arr[n + i - k] = m[i]
		return arr


class Solution:

	def leftRotate(self, arr, k, n):
		k = k % n
		arr1 = arr[k:n]
		arr2 = arr[0:k]
		arr[0:n - k] = arr1
		arr[n - k:] = arr2
		return arr


class Solution:

	def leftRotate(self, arr, k, n):
		if n <= 1:
			return arr
		if k < n:
			for _ in range(-n, -(n - k)):
				ele = arr[0]
				ele_shift = arr[1]
				arr.remove(ele)
				arr.append(ele)
			return arr
		else:
			shift = k % n
			for _ in range(-n, -(n - shift)):
				ele = arr[0]
				ele_shift = arr[1]
				arr.remove(ele)
				arr.append(ele)
			return arr


class Solution:

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

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


class Solution:

	def leftRotate(self, arr, k, n):
		rotate = k % n
		first = arr[rotate:]
		second = arr[:rotate]
		result = first + second
		for i in range(n):
			arr[i] = result[i]
		return arr


class Solution:

	def leftRotate(self, arr, k, n):
		rotate = k % n
		arr[rotate:] = reversed(arr[rotate:])
		arr[:rotate] = reversed(arr[:rotate])
		arr[:] = reversed(arr[:])
		return arr


class Solution:

	def leftRotate(self, arr, k, n):
		rotate = k % n
		arr[:] = arr[rotate:] + arr[0:rotate]
		return arr


class Solution:

	def leftRotate(self, arr, k, n):
		n = k % len(arr)
		arr[:n] = reversed(arr[:n])
		arr[n:] = reversed(arr[n:])
		arr[:] = reversed(arr[:])
		return arr


class Solution:

	def leftRotate(self, arr, k, n):
		dum_arr = [0 for _ in range(n)]
		j = 0
		k = k % n
		for i in range(k, n):
			dum_arr[j] = arr[i]
			j += 1
		for i in range(0, k):
			dum_arr[j] = arr[i]
			j += 1
		for i in range(0, n):
			arr[i] = dum_arr[i]


class Solution:

	def rev(self, arr, st, end):
		while st < end:
			(arr[st], arr[end]) = (arr[end], arr[st])
			st += 1
			end -= 1

	def leftRotate(self, arr, k, n):
		k = k % n
		self.rev(arr, 0, k - 1)
		self.rev(arr, k, n - 1)
		self.rev(arr, 0, n - 1)


class Solution:

	def leftRotate(self, arr, k, n):
		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, k, n):
		k = k % n
		a = arr[:k]
		for i in range(n - k):
			arr[i] = arr[i + k]
		i = 0
		for j in range(n - k, n):
			arr[j] = a[i]
			i = i + 1