Given an array arr[] of positive integers of size N. Reverse every sub-array group of size K.
Note: If at any instance, there are no more subarrays of size greater than or equal to K, then reverse the last subarray (irrespective of its size). You shouldn't return any array, modify the given array in-place.
Example 1:
Input:
N = 5, K = 3
arr[] = {1,2,3,4,5}
Output: 3 2 1 5 4
Explanation: First group consists of elements
1, 2, 3. Second group consists of 4,5.
 
Example 2:
Input:
N = 4, K = 3
arr[] = {5,6,8,9}
Output: 8 6 5 9
 
Your Task:
You don't need to read input or print anything. The task is to complete the function reverseInGroups() which takes the array, N and K as input parameters and modifies the array in-place. 
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ N, K ≤ 10^{7}
1 ≤ A[i] ≤ 10^{18}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function template for Python



class Solution:	

    #Function to reverse every sub-array group of size k.

	def reverseInGroups(self, arr, N, K):

		# code here

Solutions:
class Solution:

	def reverseInGroups(self, arr, N, k):
		n = len(arr)
		i = 0
		while i < n:
			if i + k <= n:
				left = i
				right = i + k - 1
			else:
				left = i
				right = n - 1
			while left < right:
				(arr[left], arr[right]) = (arr[right], arr[left])
				left += 1
				right -= 1
			i += k
		return arr


class Solution:

	def reverseInGroups(self, arr, N, K):
		i = 0
		while i < N:
			if i + K < N:
				arr[i:i + K] = arr[i:i + K][::-1]
				i += K
			else:
				arr[i:] = arr[i:N][::-1]
				i += K
		return arr


class Solution:

	def reverseInGroups(self, arr, N, K):
		i = 0
		while i < N:
			if i + K < N:
				arr[i:i + K] = reversed(arr[i:i + K])
				i += K
			else:
				arr[i:] = reversed(arr[i:])
				i += K


class Solution:

	def reverseInGroups(self, arr, N, K):
		for i in range(0, N, K):
			if i < N - K:
				arr[i:i + K] = arr[i:i + K][::-1]
			else:
				arr[i:] = arr[i:][::-1]
		return arr


class Solution:

	def reverseInGroups(self, arr, N, K):
		start = 0
		end = K
		while start < N:
			new_list = arr[start:end]
			new_list.reverse()
			j = start
			for item in new_list:
				arr[j] = item
				j += 1
			start = end
			end = end + K
		return arr


class Solution:

	def reverseInGroups(self, arr, N, K):
		i = 0
		while i + K < N:
			arr[i:i + K] = arr[i:i + K][::-1]
			i += K
		arr[i:] = arr[i:][::-1]


class Solution:

	def reverseInGroups(self, arr, N, K):
		c = 0
		l1 = [0]
		for i in range(len(arr)):
			c += 1
			if c > K:
				l1.append(i)
				c = 1
		if c > 0 and c < K:
			l1.append(len(arr))
		for i in range(len(l1) - 1):
			arr[l1[i]:l1[i + 1]] = arr[l1[i]:l1[i + 1]][::-1]
		return arr


class Solution:

	def reverseInGroups(self, arr, N, K):
		for i in range(0, N, K):
			start = i
			end = start + K - 1
			if start + K - 1 > N - 1:
				end = N - 1
			while start < end:
				(arr[start], arr[end]) = (arr[end], arr[start])
				start += 1
				end -= 1


class Solution:

	def reverseInGroups(self, arr, N, K):
		i = 0
		while i < N:
			left = i
			right = min(i + K - 1, N - 1)
			while left < right:
				(arr[left], arr[right]) = (arr[right], arr[left])
				left += 1
				right -= 1
			i += K


class Solution:

	def reverseInGroups(self, arr, N, k):
		start = 0
		end = k - 1
		while end < N:
			tempStart = start
			tempEnd = end
			while tempStart < tempEnd:
				(arr[tempStart], arr[tempEnd]) = (arr[tempEnd], arr[tempStart])
				tempEnd -= 1
				tempStart += 1
			start = end + 1
			end += k
		if start < N:
			end = N - 1
			while start < end:
				(arr[start], arr[end]) = (arr[end], arr[start])
				start += 1
				end -= 1


class Solution:

	def reverseInGroups(self, arr, N, K):
		N = N % K
		if len(arr) <= K:
			first = arr[:N + 1]
			first = first[::-1]
			second = arr[N + 1:]
			second = second[::-1]
			arr[:] = first + second
			return arr
		else:
			lst = []
			length = len(arr)
			divide = length // K
			a = 0
			b = K
			for i in range(divide + 1):
				res = arr[a:b][::-1]
				for i in res:
					lst.append(i)
				a = b
				b += K
		arr[:] = lst
		return arr


class Solution:

	def reverseInGroups(self, arr, N, K):
		for i in range(0, N, K):
			arr[i:i + K] = arr[i:i + K][::-1]


class Solution:

	def reverseInGroups(self, arr, N, K):
		for i in range(0, N, K):
			left = i
			right = min(i + K - 1, N - 1)
			while left < right:
				(arr[left], arr[right]) = (arr[right], arr[left])
				left += 1
				right -= 1


class Solution:

	def reverseInGroups(self, arr, n, k):
		for i in range(0, n, k):
			arr[i:i + k] = arr[i:i + k][::-1]
		return arr


class Solution:

	def reverseInGroups(self, arr, N, K):
		for i in range(0, N, K):
			p = i + K - 1
			if p > N - 1:
				p = N - 1
			while p > i:
				(arr[i], arr[p]) = (arr[p], arr[i])
				p -= 1
				i += 1


class Solution:

	def reverseInGroups(self, arr, N, K):
		a = []
		s = 0
		i = 0
		d = 0
		while s <= N:
			i += 1
			if i == K or s == N:
				a += arr[d:d + i][::-1]
				d += K
				i = 0
			s += 1
		return a


class Solution:

	def reverseInGroups(self, arr, N, K):
		a = []
		jump = K
		for i in range(0, N):
			if jump <= N + K:
				a += arr[K * i:jump][::-1]
				jump += K
			else:
				arr[:] = a
				break


class Solution:

	def reverseInGroups(self, arr, N, K):
		z = 0
		while z < N - 1:
			(i, j) = (z, min(z + K - 1, N - 1))
			while i < j:
				[arr[i], arr[j]] = [arr[j], arr[i]]
				i += 1
				j -= 1
			z = min(z + K, N - 1)


class Solution:

	def reverseInGroups(self, arr, N, K):
		start = 0
		while start < N:
			end = min(start + K - 1, N - 1)
			left = start
			right = end
			while left < right:
				(arr[left], arr[right]) = (arr[right], arr[left])
				left += 1
				right -= 1
			start += K
		return arr


class Solution:

	def reverseInGroups(self, arr, N, K):
		for i in range(0, len(arr), K):
			group = arr[i:i + K]
			group.reverse()
			arr[i:i + K] = group


class Solution:

	def reverseInGroups(self, arr, N, K):
		for i in range(0, N, K):
			arr[i:i + K] = reversed(arr[i:i + K])
		return arr


class Solution:

	def reverseInGroups(self, arr, N, k):
		ans = []
		for x in range(0, len(arr), k):
			l = arr[x:x + k][::-1]
			ans += l
		i = 0
		for x in ans:
			arr[i] = x
			i += 1


class Solution:

	def reverseInGroups(self, arr, N, K):
		i = 0
		while i < N:
			start = i
			end = min(i + K - 1, N - 1)
			while start < end:
				(arr[start], arr[end]) = (arr[end], arr[start])
				start += 1
				end -= 1
			i += K
		return arr


class Solution:

	def reverseInGroups(self, arr, n, k):
		start = 0
		end = k - 1
		while end < n:
			tempstart = start
			tempend = end
			while tempstart < tempend:
				(arr[tempstart], arr[tempend]) = (arr[tempend], arr[tempstart])
				tempstart += 1
				tempend -= 1
			start = end + 1
			end = end + k
		if start < n:
			end = n - 1
			tempstart = start
			tempend = end
			while tempstart < tempend:
				(arr[tempstart], arr[tempend]) = (arr[tempend], arr[tempstart])
				tempstart += 1
				tempend -= 1


class Solution:

	def reverseInGroups(self, arr, N, K):

		def rev_arr(arr, low, high):
			while low <= high:
				(arr[low], arr[high]) = (arr[high], arr[low])
				low += 1
				high -= 1
		for i in range(0, N, K):
			try:
				rev_arr(arr, i, i + K - 1)
			except IndexError:
				rev_arr(arr, i, N - 1)
			else:
				pass


class Solution:

	def reverseInGroups(self, arr, n, k):
		i = 0
		while i < n:
			left = i
			right = min(i + k - 1, n - 1)
			while left < right:
				(arr[left], arr[right]) = (arr[right], arr[left])
				left += 1
				right -= 1
			i += k
		return arr


class Solution:

	def reverseInGroups(self, arr, N, K):
		x = 0
		while x < N:
			l = x
			if N - 1 >= x + K - 1:
				h = x + K - 1
			else:
				h = N - 1
			while l < h:
				(arr[h], arr[l]) = (arr[l], arr[h])
				h = h - 1
				l = l + 1
			x = x + K


class Solution:

	def reverseInGroups(self, arr, N, K):
		ans = []
		r = N / K
		for i in range(0, N, K):
			end = i + K
			if end > N:
				end = N
			temp = arr[i:end]
			temp.reverse()
			arr[i:end] = temp


class Solution:

	def reverseInGroups(self, arr, N, K):
		i = 0
		while i < N:
			l = i
			r = min(N - 1, i + K - 1)
			while l < r:
				(arr[l], arr[r]) = (arr[r], arr[l])
				l += 1
				r -= 1
			i = i + K


class Solution:

	def reverseInGroups(self, arr, N, K):
		for i in range(0, N, K):
			if i + K <= N:
				arr[i:i + K] = reversed(arr[i:i + K])
			else:
				arr[i:] = reversed(arr[i:])
				break


class Solution:

	def reverseInGroups(self, arr, n, k):
		i = 0
		while i < n:
			end = i + k - 1 if i + k - 1 < n else n - 1
			start = i
			while start < end:
				(arr[start], arr[end]) = (arr[end], arr[start])
				start += 1
				end -= 1
			i += k


class Solution:

	def swap(self, arr, i, j):
		t = arr[i]
		arr[i] = arr[j]
		arr[j] = t

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

	def reverseInGroups(self, arr, N, K):
		start = 0
		while start < N:
			end = start + K
			if end < N:
				end -= 1
			else:
				end = N - 1
			self.reverse(arr, start, end)
			start += K


import math

class Solution:

	def reverseInGroups(self, arr, N, K):
		self.arr = arr
		self.N = N
		self.K = K
		nn = int(math.ceil(N / K))
		for i in range(nn):
			ini = i * K
			fin = i * K + K
			sub_arr = arr[ini:fin]
			sub_arr.reverse()
			arr[ini:fin] = sub_arr
		return arr


class Solution:

	def reverseInGroups(self, arr, n, k):
		i = 0
		while i < n:
			j = i + k - 1
			if j >= n:
				j = n - 1
			left = i
			right = j
			while left < right:
				(arr[left], arr[right]) = (arr[right], arr[left])
				left += 1
				right -= 1
			i += k


class Solution:

	def reverseInGroups(self, a, N, k):
		i = 0
		while i < N:
			a[i:i + k] = a[i:i + k][::-1]
			i = i + k
		return a


class Solution:

	def reverseInGroups(self, arr, N, K):
		gp = N // K
		rm = N % K
		if gp != 0:
			for i in range(gp):
				for j in range(1, K // 2 + 1):
					(arr[i * K + j - 1], arr[i * K + K - j]) = (arr[i * K + K - j], arr[i * K + j - 1])
			for n in range(rm // 2):
				(arr[K * gp + n], arr[K * gp + rm - 1 - n]) = (arr[K * gp + rm - 1 - n], arr[K * gp + n])
		else:
			for n in range(N // 2):
				(arr[n], arr[N - n - 1]) = (arr[N - n - 1], arr[n])
		return arr