Given an unsorted array arr[] of size N. Rotate the array to the left (counter-clockwise direction) by D steps, where D is a positive integer. 
 
Example 1:
Input:
N = 5, D = 2
arr[] = {1,2,3,4,5}
Output: 3 4 5 1 2
Explanation: 1 2 3 4 5  when rotated
by 2 elements, it becomes 3 4 5 1 2.
Example 2:
Input:
N = 10, D = 3
arr[] = {2,4,6,8,10,12,14,16,18,20}
Output: 8 10 12 14 16 18 20 2 4 6
Explanation: 2 4 6 8 10 12 14 16 18 20 
when rotated by 3 elements, it becomes 
8 10 12 14 16 18 20 2 4 6.
 
Your Task:
Complete the function rotateArr() which takes the array, D and N as input parameters and rotates the array by D elements. The array must be modified in-place without using extra space. 
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 10^{6}
1 <= D <= 10^{6}
0 <= arr[i] <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



class Solution:

    #Function to rotate an array by d elements in counter-clockwise direction. 

    def rotateArr(self,A,D,N):

        #Your code here

Solutions:
class Solution:

	def rotateArr(self, A, D, N):
		D = D % N
		L = A[:D]
		for i in range(D, N):
			A[i - D] = A[i]
		for i in range(N - D, N):
			A[i] = L[i - (N - D)]


class Solution:

	def rotateArr(self, A, D, N):
		while D > N:
			D = D - N
		l = A[D:] + A[:D]
		A[:] = l[:]
		return A


class Solution:

	def rotateArr(self, A, D, N):
		D = D % len(A)
		temp = A[:D]
		for i in range(D, len(A)):
			A[i - D] = A[i]
		for i in range(D):
			A[len(A) - D + i] = temp[i]


class Solution:

	def rotateArr(self, A, D, N):
		D = D % N
		self.reverse(A, 0, D - 1)
		self.reverse(A, D, N - 1)
		self.reverse(A, 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 rotateArr(self, A, D, N):
		D = D % N
		self.reverseA(A, 0, D - 1)
		self.reverseA(A, D, N - 1)
		self.reverseA(A, 0, N - 1)

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


class Solution:

	def rotateArr(self, A, D, N):
		ans = []
		for i in range(N):
			ans.append(A[i])
		for j in range(N):
			A[(j - D) % N] = ans[j]


class Solution:

	def rotateArr(self, arr, k, n):
		if k == 0:
			return
		elif k == n:
			return
		else:
			if k > n:
				k = k % n
			l = 0
			r = k - 1
			while l <= r:
				temp = arr[l]
				arr[l] = arr[r]
				arr[r] = temp
				l += 1
				r -= 1
			l = k
			r = n - 1
			while l <= r:
				temp = arr[l]
				arr[l] = arr[r]
				arr[r] = temp
				l += 1
				r -= 1
			l = 0
			r = n - 1
			while l <= r:
				temp = arr[l]
				arr[l] = arr[r]
				arr[r] = temp
				l += 1
				r -= 1
			return


class Solution:

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

	def rotateArr(self, A, D, N):
		D = D % N
		self.r(A, 0, D - 1)
		self.r(A, D, N - 1)
		self.r(A, 0, N - 1)


class Solution:

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

	def rotateArr(self, A, D, N):
		l = D % N
		self.revse(A, 0, l - 1)
		self.revse(A, l, N - 1)
		self.revse(A, 0, N - 1)
		return A


class Solution:

	def rotateArr(self, A, D, N):
		D %= N
		A[0:D] = reversed(A[0:D])
		A[D:N] = reversed(A[D:N])
		A[0:N] = reversed(A[0:N])


class Solution:

	def rotateArr(self, arr, D, N):
		D = D % N
		k = arr[D:] + arr[0:D]
		for i in range(N):
			arr[i] = k[i]
		return arr


class Solution:

	def rotateArr(self, A, D, N):
		if D > N:
			D = D % N
		k = A[D:] + A[:D]
		for i in range(N):
			A[i] = k[i]


class Solution:

	def rotateArr(self, A, D, N):
		D = D % N
		A.reverse()
		i = 0
		j = N - D - 1
		while i < j:
			(A[i], A[j]) = (A[j], A[i])
			i += 1
			j -= 1
		i = N - D
		j = N - 1
		while i < j:
			(A[i], A[j]) = (A[j], A[i])
			i += 1
			j -= 1
		return A


class Solution:

	def rotateArr(self, A, D, N):
		D = D % N
		arr = A[D:] + A[:D]
		A.clear()
		A.extend(arr)


class Solution:

	def __reverseArr(self, A, start, end):
		while start < end:
			temp = A[start]
			A[start] = A[end]
			A[end] = temp
			start = start + 1
			end = end - 1
		return A

	def rotateArr(self, A, D, N):
		if D > N:
			D = D % N
		A = self.__reverseArr(A, 0, D - 1)
		A = self.__reverseArr(A, D, N - 1)
		A = self.__reverseArr(A, 0, N - 1)
		return A


import copy

class Solution:

	def rotateArr(self, A, D, N):
		D = D % N
		A[:D] = reversed(A[:D])
		A[D:] = reversed(A[D:])
		A[:] = reversed(A[:])


class Solution:

	def rotateArr(self, A, D, N):
		D = D % N
		A[:] = A[D:] + A[:D]
		return A[:]


class Solution:

	def rotateArr(self, A, D, N):
		if D > N:
			D = D % N
		A[:D] = list(reversed(A[:D]))
		A[D:] = list(reversed(A[D:]))
		A[:] = reversed(A[:])
		return list(A)


class Solution:

	def rotateArr(self, a, d, n):
		d = d % n
		i = 0
		t = []
		while i < d:
			t.append(a[i])
			i += 1
		for i in range(n - d):
			a[i] = a[i + d]
		i = 0
		while i < d:
			a[n - d + i] = t[i]
			i += 1
		return a


class Solution:

	def rotateArr(self, A, D, N):
		List1 = A
		D %= N
		List1[:] = A[D:] + A[0:D]
		return List1


class Solution:

	def rotateArr(self, A, D, N):
		D = D % N
		if D == 0:
			return
		i = 0
		j = D - 1
		while i < j:
			temp = A[i]
			A[i] = A[j]
			A[j] = temp
			i += 1
			j -= 1
		i = D
		j = N - 1
		while i < j:
			temp = A[i]
			A[i] = A[j]
			A[j] = temp
			i += 1
			j -= 1
		i = 0
		j = N - 1
		while i < j:
			temp = A[i]
			A[i] = A[j]
			A[j] = temp
			i += 1
			j -= 1


class Solution:

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

	def rotateArr(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)


class Solution:

	def rotateArr(self, A, D, N):
		D %= N
		if D < 0:
			D += N

		def rev(left, right):
			while left < right:
				(A[left], A[right]) = (A[right], A[left])
				left += 1
				right -= 1
		rev(0, D - 1)
		rev(D, N - 1)
		rev(0, N - 1)


class Solution:

	def rotateArr(self, A, D, N):
		if D > N:
			D = D % N
		one = A[:D]
		two = A[D:]
		A[:] = two + one


class Solution:

	def rotateArr(self, A, D, N):
		if D > N:
			D = D % N
		A[:] = A[D:] + A[:D]


class Solution:

	def rotateArr(self, A, D, N):
		for i in range(D):
			A.append(A[i])
		del A[0:D]
		return A


class Solution:

	def rotateArr(self, arr, steps, n):
		if steps > n:
			steps = steps % n
		tempArr = arr[steps:] + arr[:steps]
		for i in range(n):
			arr[i] = tempArr[i]


class Solution:

	def rotateArr(self, v, d, n):
		if d > n:
			d = d % n
		v[:] = v[d:] + v[:d]
		v = v[::-1]


class Solution:

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

	def rotateArr(self, A, D, N):
		diff = D % N
		self.reverse(A, 0, diff - 1)
		self.reverse(A, diff, N - 1)
		self.reverse(A, 0, N - 1)


class Solution:

	def rotateArr(self, arr, d, N):
		if d > N:
			d = d % N
		arr[:] = arr[d:] + arr[:d]
		return arr


class Solution:

	def rotateArr(self, A, D, N):
		D = D % N
		if D == 0:
			return A
		reverse(A, 0, D - 1)
		reverse(A, D, N - 1)
		reverse(A, 0, N - 1)
		return A

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


class Solution:

	def rotateArr(self, A, D, N):
		if D > N:
			D = D % N
		arr = A[D:] + A[0:D]
		for i in range(N):
			A[i] = arr[i]


class Solution:

	def reverse(self, arr):
		n = len(arr)
		for i in range(n // 2):
			(arr[i], arr[n - i - 1]) = (arr[n - i - 1], arr[i])
		return arr

	def rotateArr(self, A, D, N):
		D = D % N
		A[:D] = self.reverse(A[:D])
		A[D:] = self.reverse(A[D:])
		A = self.reverse(A)


class Solution:

	def rotateArr(self, A, D, N):
		D = D % N
		if D == 0:
			return
		d = [A[i] for i in range(D)]
		for i in range(D, N):
			A[i - D] = A[i]
		for i in range(D):
			A[N - D + i] = d[i]


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

class Solution:

	def rotateArr(self, arr, d, n):
		d = d % n
		g_c_d = gcd(d, n)
		for i in range(g_c_d):
			temp = arr[i]
			j = i
			while 1:
				k = j + d
				if k >= n:
					k = k - n
				if k == i:
					break
				arr[j] = arr[k]
				j = k
			arr[j] = temp


class Solution:

	def rotateArr(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)

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


class Solution:

	def rotateArr(self, A, D, N):
		if D > N:
			d = D % N
		else:
			d = D
		temp = A[0:d]
		del A[0:d]
		A = A.extend(temp)
		return A


class Solution:

	def rotateArr(self, A, D, N):
		cnt = 0
		while D > N:
			D = D - N
			cnt += 1
		B = A[:D]
		for i in range(N):
			if i < N - D:
				A[i] = A[i + D]
			else:
				A[i] = B[i - (N - D)]
		return A


class Solution:

	def rotateArr(self, array, D, n):
		D = D % n
		times = self.gcd(D, n)
		for i in range(times):
			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
		return None

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


class Solution:

	def rotateArr(self, A, D, N):

		def reverse(arr, low, high):
			while low < high:
				temp = arr[low]
				arr[low] = arr[high]
				arr[high] = temp
				low += 1
				high -= 1
		D = D % N
		reverse(A, 0, N - 1)
		reverse(A, 0, N - D - 1)
		reverse(A, N - D, N - 1)


class Solution:

	def reverseArr(self, arr, start, end):
		times = int((end - start + 1) / 2)
		i = 0
		while times > 0:
			(arr[start + i], arr[end - i]) = (arr[end - i], arr[start + i])
			i += 1
			times = times - 1

	def rotateArr(self, arr, D, N):
		if len(arr) == 1:
			return arr
		D = D % N
		self.reverseArr(arr, 0, D - 1)
		self.reverseArr(arr, D, N - 1)
		self.reverseArr(arr, 0, N - 1)


class Solution:

	def rotateArr(self, arr, D, N):
		D = D % N
		l = 0
		r = D - 1
		while l < r:
			(arr[l], arr[r]) = (arr[r], arr[l])
			l += 1
			r -= 1
		l = D
		r = N - 1
		while l < r:
			(arr[l], arr[r]) = (arr[r], arr[l])
			l += 1
			r -= 1
		return arr.reverse()


import numpy as np

class Solution:

	def rotateArr(self, A, D, N):
		if D > N:
			D = D % N
		x = [0] * D
		for i in range(D):
			x[i] = A[i]
		res = 0
		for j in range(D, len(A)):
			A[res] = A[j]
			res += 1
		for k in x:
			A[res] = k
			res += 1
		return A


class Solution:

	def rotateArr(self, a, d, n):
		d = d % n
		arr = a[:d]
		for i in range(n - d):
			a[i] = a[i + d]
		j = 0
		for i in range(n - d, n):
			a[i] = arr[j]
			j += 1


from collections import deque

class Solution:

	def rotateArr(self, a, d, n):
		d = d % n
		arr = a[:d]
		for i in range(n - d):
			a[i] = a[i + d]
		j = 0
		for i in range(n - d, n):
			a[i] = arr[j]
			j += 1


class Solution:

	def rotateArr(self, A, D, N):
		if N <= 0:
			return
		lst = []
		D = D % N
		lst = A[D:] + A[0:D]
		for i in range(len(lst)):
			A[i] = lst[i]
		return A


import math

class Solution:

	def rotateArr(self, A, D, N):
		D = D % N
		self.reverse(A, 0, D - 1)
		self.reverse(A, D, N - 1)
		self.reverse(A, 0, N - 1)

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


import math

class Solution:

	def rotateArr(self, A, D, N):
		i = 0
		if D > N:
			D = D % N
		K = A[D:] + A[:D]
		for i in range(N):
			A[i] = K[i]


class Solution:

	def rotateArr(self, A, D, N):
		if D < N:
			A[:] = A[D:N] + A[0:D]
			return A
		elif D - N < 70:
			K = D - N
			A[:] = A[K:N] + A[0:K]
			return A
		else:
			k = D % N
			A[:] = A[k:N] + A[0:k]
			return A


class Solution:

	def rotateArr(self, A, D, N):
		D = D % N
		l = A[D:]
		l1 = A[:D]
		A.clear()
		for i in l:
			A.append(i)
		for i in l1:
			A.append(i)


class Solution:

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

	def rotateArr(self, arr, k, n):
		k = k % n - 1
		self.helper(arr, 0, k)
		self.helper(arr, k + 1, n - 1)
		arr.reverse()