Given an integer K and a queue of integers, we need to reverse the order of the first K elements of the queue, leaving the other elements in the same relative order.
Only following standard operations are allowed on queue.
	enqueue(x) : Add an item x to rear of queue
	dequeue() : Remove an item from front of queue
	size() : Returns number of elements in queue.
	front() : Finds front item.
	Note: The above operations represent the general processings. In-built functions of the respective languages can be used to solve the problem.
Example 1:
Input:
5 3
1 2 3 4 5
Output: 
3 2 1 4 5
Explanation: 
After reversing the given
input from the 3rd position the resultant
output will be 3 2 1 4 5.
Example 2:
Input:
4 4
4 3 2 1
Output: 
1 2 3 4
Explanation: 
After reversing the given
input from the 4th position the resultant
output will be 1 2 3 4.
Your Task:
Complete the provided function modifyQueue that takes queue and k as parameters and returns a modified queue. The printing is done automatically by the driver code.
Expected Time Complexity : O(N)
Expected Auxiliary Space : O(K)
Constraints:
1 <= N <= 1000
1 <= K <= N

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3

'''

Function Arguments :

		@param  : q ( given queue to be used), k(Integer )

		@return : list, just reverse the first k elements of queue and return new queue

'''



#Function to reverse first k elements of a queue.

def modifyQueue(q,k):

    # code here

Solutions:
def modifyQueue(q, k):
	l = []
	n = len(q)
	for i in range(k):
		l.append(q.pop(0))
	for i in range(k):
		q.append(l[-1])
		del l[-1]
	for i in range(n - k):
		q.append(q.pop(0))
	return q


def modifyQueue(q, k):
	st = []
	for i in range(k):
		st.append(q[i])
	for i in range(k):
		q[i] = st.pop()
	return q


def modifyQueue(q, k):
	n = len(q)
	stack = []
	for i in range(k):
		val = q.pop(0)
		stack.append(val)
	for i in range(k):
		val = stack.pop()
		q.append(val)
	for i in range(n - k):
		val = q.pop(0)
		q.append(val)
	return q


def modifyQueue(q, k):
	s = q[:k]
	t = q[k:]
	r = s[::-1] + t
	return r


def modifyQueue(q, k):
	return q[:k][::-1] + q[k:]


def modifyQueue(q, k):
	x = 0
	n = k - 1
	while x < n:
		(q[x], q[n]) = (q[n], q[x])
		x += 1
		n -= 1
	return q


def modifyQueue(q, k):
	st = []
	ans = []
	for i in range(k):
		st.append(q[i])
	while st:
		ans.insert(0, st.pop(0))
	for j in range(k, n):
		ans.append(q[j])
	return ans


def modifyQueue(q, k):
	(arr1, arr2) = (q[:k][::-1], q[k:])
	return arr1 + arr2


def modifyQueue(q, k):
	for i in range(k // 2):
		(q[i], q[k - i - 1]) = (q[k - i - 1], q[i])
	return q


def modifyQueue(q, k):
	s = []
	n = len(q)
	for i in range(k):
		s.append(q.pop(0))
	for i in range(k):
		q.append(s.pop())
	for i in range(n - k):
		q.append(q.pop(0))
	return q


def modifyQueue(q, k):
	q[:k] = reversed(q[:k])
	return q


def modifyQueue(q, k):
	stack = []
	length = len(q)
	for i in range(k):
		stack.append(q[0])
		q.pop(0)
	while stack:
		q.append(stack.pop())
	for i in range(length - k):
		q.append(q.pop(0))
	return q


def rev(q, i):
	if not i:
		return
	temp = q.pop(0)
	rev(q, i - 1)
	q.append(temp)

def modifyQueue(q, k):
	n = len(q)
	rev(q, k)
	i = 0
	while i < n - k:
		q.append(q.pop(0))
		i += 1
	return q


def modifyQueue(q, k):
	temp = []
	for i in range(k):
		temp.append(q[i])
	for i in range(k):
		q[i] = temp.pop()
	return q


def modifyQueue(q, k):
	Q = []
	for i in range(k):
		Q.append(q.pop(0))
	Q = Q[::-1]
	Q = Q + q
	return Q


def modifyQueue(q, k):
	n = len(q)
	for i in range(0, n, k):
		if i <= n - k:
			q[0:k] = q[0:k][::-1]
		else:
			q[k:] = q[k:]
		return q


def modifyQueue(q, k):
	st = []
	i = 0
	while i != k:
		st.append(q[i])
		i += 1
	frnt = -1
	while len(st) > 0:
		tmp = st.pop()
		frnt = (frnt + 1) % n
		q[frnt] = tmp
	return q


def modifyQueue(q, k):
	i = 0
	j = k - 1
	while i < j:
		(q[i], q[j]) = (q[j], q[i])
		i += 1
		j -= 1
	return q


def modifyQueue(q, k):
	l = q[:k][::-1]
	l1 = q[k:]
	return l + l1


def modifyQueue(q, k):
	n = len(q)
	st = []
	for i in range(k):
		nums = q.pop(0)
		st.append(nums)
	while st:
		q.append(st.pop())
	for i in range(n - k):
		nums = q.pop(0)
		st.append(nums)
	while st:
		q.append(st.pop(0))
	return q


def modifyQueue(q, k):
	stack = []
	q2 = []
	count = 0
	while count < k:
		stack.append(q.pop(0))
		count += 1
	while stack:
		q2.append(stack.pop())
	while q:
		q2.append(q.pop(0))
	return q2


def modifyQueue(q, k):
	stack = []
	n = len(q)
	for _ in range(k):
		stack.append(q.pop(0))
	for _ in range(k):
		q.append(stack.pop())
	for _ in range(n - k):
		x = q.pop(0)
		q.append(x)
	return q


def modifyQueue(q, k):
	newArr = []
	for i in range(k):
		newArr.insert(0, q[i])
	for i in range(k, len(q)):
		newArr.append(q[i])
	return newArr


def modifyQueue(q, k):
	rev(q, k)
	for i in range(len(q) - k):
		q.append(q.pop(0))
	return q

def rev(q, k):
	if k <= 0 or len(q) == 0:
		return
	tmp = q.pop(0)
	rev(q, k - 1)
	q.append(tmp)


def modifyQueue(q, k):
	stk1 = []
	stk2 = []
	for i in range(k):
		stk1.append(q.pop(0))
	while q:
		stk2.append(q.pop())
	while stk1:
		q.append(stk1.pop())
	while stk2:
		q.append(stk2.pop())
	return q


def modifyQueue(q, k):
	stack = []
	n = len(q)
	count = k
	while k != 0:
		stack.append(q.pop(0))
		k -= 1
	while k < count:
		q.append(stack.pop())
		k += 1
	count = n - k
	while count:
		q.append(q.pop(0))
		count -= 1
	return q


def modifyQueue(q, k):
	l = []
	for i in range(k):
		l.append(q[i])
	l.reverse()
	l += q[k:]
	return l


def modifyQueue(q, k):
	stack = []
	answer = []
	for i in range(0, k):
		stack.append(q.pop(0))
	while stack:
		answer.append(stack[-1])
		stack.pop()
	while q:
		answer.append(q[0])
		q.pop(0)
	return answer


def modifyQueue(q, k):
	stack = []
	left = 0
	right = k - 1
	while left < right:
		(q[left], q[right]) = (q[right], q[left])
		left = left + 1
		right = right - 1
	return q


def modifyQueue(q, k):
	a = q[0:k]
	b = q[k:]
	x = a[::-1]
	return x + b


def modifyQueue(q, k):
	stack = []
	count = k
	while count:
		stack.append(q.pop(0))
		count -= 1
	for i in range(len(stack) - 1, -1, -1):
		q.append(stack[i])
	size = len(q) - k
	for i in range(size):
		q.append(q.pop(0))
	return q


def modifyQueue(q, k):
	t = []
	for i in range(k):
		t.append(q[i])
	t.reverse()
	for i in range(k):
		q[i] = t[i]
	return q


def modifyQueue(q, k):
	if k == len(q):
		q.reverse()
		return q
	else:
		r = q[0:k]
		return r[::-1] + q[k:]


def modifyQueue(q, k):
	stack = []
	qq = []
	for i in range(k):
		stack.append(q.pop(0))
	while len(stack):
		qq.insert(0, stack.pop(0))
	while q:
		qq.append(q.pop(0))
	return qq


from queue import Queue

def reverse_k(queue: Queue, k: int):
	data_stack = []
	for i in range(0, k):
		data_stack.append(queue.pop(0))
	while len(data_stack) > 0:
		queue.append(data_stack.pop(-1))
	n = len(queue)
	for i in range(0, n - k):
		x = queue.pop(0)
		queue.append(x)
	return queue

def modifyQueue(q, k):
	return reverse_k(q, k)


def modifyQueue(q, k):
	l = []
	i = 0
	while i < k:
		a = q.pop(0)
		l.append(a)
		i += 1
	j = 0
	while j < k:
		a = l.pop(0)
		q.insert(0, a)
		j += 1
	return q


def modifyQueue(q, k):
	ans = []
	stack = []
	c = 0
	while c < k:
		stack.append(q.pop(0))
		c += 1
	while len(stack) > 0:
		ans.append(stack.pop())
	for i in q:
		ans.append(i)
	return ans


def modifyQueue(q, k):
	n = len(q)
	stk = []
	lst = []
	for i in range(k):
		stk.append(q[i])
	while len(stk) != 0:
		lst.append(stk.pop())
	for i in range(k, n):
		lst.append(q[i])
	return lst


def modifyQueue(q, k):
	stk = []
	ans = []
	while k:
		stk.append(q.pop(0))
		k -= 1
	while len(stk):
		ans.append(stk.pop())
	while len(q):
		ans.append(q.pop(0))
	return ans


def modifyQueue(q, k):
	stack = []
	for i in range(k):
		x = q.pop(0)
		stack.insert(0, x)
	while stack:
		q.insert(0, stack.pop())
	return q


def modifyQueue(q, k):
	l = []
	for i in range(k - 1, -1, -1):
		l.append(q[i])
	for i in range(k, len(q)):
		l.append(q[i])
	return l


def modifyQueue(q, k):
	if len(q) >= k:
		t = q[:k]
		t = t[::-1]
		q[:k] = t
	return q


def modifyQueue(q, k):
	i = 0
	l = []
	while i < k:
		num = q.pop(0)
		l.append(num)
		i += 1
	while l:
		num = l.pop()
		q.append(num)
	t = len(q) - k
	while t:
		num = q.pop(0)
		q.append(num)
		t -= 1
	return q


from collections import deque

def modifyQueue(q, k):
	stack = []
	for i in range(k):
		stack.append(q.pop(0))
	ans = deque()
	while stack:
		ans.append(stack.pop())
	while q:
		ans.append(q.pop(0))
	return ans


def modifyQueue(q, k):
	st = 0
	end = k - 1
	while st <= end:
		(q[st], q[end]) = (q[end], q[st])
		st += 1
		end -= 1
	return q


def modifyQueue(q, k):
	temp = []
	i = k - 1
	while i >= 0:
		temp.append(q[i])
		i -= 1
	final = []
	final = temp + q[k:]
	return final


def modifyQueue(q, k):
	i = 0
	if k % 2 == 0:
		while k > 0:
			(q[i], q[k - 1]) = (q[k - 1], q[i])
			k -= 1
			i += 1
			if i == k:
				break
	else:
		while k > 0:
			(q[i], q[k - 1]) = (q[k - 1], q[i])
			k -= 1
			i += 1
			if i == k - 1:
				break
	return q


def modifyQueue(q, k):
	stack = []
	newqueue = []
	while k:
		stack.append(q.pop(0))
		k -= 1
	while len(stack):
		newqueue.insert(0, stack.pop(0))
	while len(q):
		newqueue.append(q.pop(0))
	return newqueue


def modifyQueue(q, k):
	q1 = list(q[:k])
	q2 = list(q[k:])
	q1.reverse()
	return q1 + q2


def modifyQueue(q, k):
	st = []
	for i in range(k):
		a = q.pop(0)
		st.append(a)
	while len(st) != 0:
		a = st.pop(0)
		q.insert(0, a)
	return q


import queue

def modifyQueue(q, k):
	stack = []
	for i in range(k):
		stack.append(q[0])
		q.pop(0)
	while stack:
		q.append(stack.pop())
	for i in range(len(q) - k):
		q.append(q.pop(0))
	return q


def modifyQueueuntil(q, k):
	if len(q) == 0 or k <= 0:
		return q
	a = q.pop(0)
	modifyQueueuntil(q, k - 1)
	q.append(a)
	return q

def modifyQueue(q, k):
	modifyQueueuntil(q, len(q))
	modifyQueueuntil(q, len(q) - k)
	return q


def modifyQueue(q, k):
	x = [None] * k
	for i in range(k):
		y = q.pop(0)
		x[i] = y
	r = -1
	for i in range(k):
		r = (r + 1) % k
		y = x.pop(-1)
		q.insert(r, y)
	return q


def modifyQueue(q, k):
	start = 0
	end = k - 1
	while start < end:
		(q[start], q[end]) = (q[end], q[start])
		start += 1
		end -= 1
	return q


def modifyQueue(q, k):
	stack = []
	i = 0
	while i < k:
		temp = q.pop(0)
		stack.append(temp)
		i += 1
	while stack:
		temp = stack.pop(0)
		q.insert(0, temp)
	return q


import collections

def modifyQueue(q, k):
	q = collections.deque(q)
	solve(q, k)
	s = len(q) - k
	for i in range(s):
		x = q.popleft()
		q.append(x)
	return q

def solve(q, k):
	if k == 0:
		return
	e = q.popleft()
	solve(q, k - 1)
	q.append(e)


def modifyQueue(q, k):
	N = len(q)
	stack = []
	for _ in range(k):
		stack.append(q.pop(0))
	while len(stack) > 0:
		q.append(stack.pop())
	for _ in range(N - k):
		temp = q.pop(0)
		q.append(temp)
	return q


def modifyQueue(q, k):
	ap = []
	for x in range(k):
		ap.append(q[x])
	ap.reverse()
	for x in range(len(q) - k):
		ap.append(q[x + k])
	return ap


def modifyQueue(q, k):
	solve(q, k)
	for i in range(len(q) - k):
		a = q.pop(0)
		q.append(a)
	return q

def solve(q, k):
	if k == 0:
		return
	e = q.pop(0)
	solve(q, k - 1)
	q.append(e)


def modifyQueue(q, k):
	stack = []
	for i in range(k):
		stack.append(q.pop(0))
	stack = stack[::-1]
	result = stack + q
	return result


from queue import Queue

def modifyQueue(q, k):
	res_queue = []
	stack = []
	for i in range(k):
		ele = q.pop(0)
		stack.append(ele)
	while stack:
		res_queue.append(stack[-1])
		stack.pop(-1)
	while q:
		res_queue.append(q.pop(0))
	return res_queue


def modifyQueue(q, k):
	arr = []
	for i in range(k):
		arr.append(q.pop(0))
	for j in arr:
		q.insert(0, j)
	return q


def modifyQueue(q, k):
	temp = []
	siz = len(q)
	for i in range(k):
		x = q.pop(0)
		temp.insert(0, x)
	for i in range(k):
		q.append(temp[i])
	for i in range(siz - k):
		x = q.pop(0)
		q.append(x)
	return q


from queue import LifoQueue

def modifyQueue(q, k):
	stack = LifoQueue(maxsize=k)
	for i in range(k):
		stack.put(q.pop(0))
	i = 0
	while not stack.empty():
		q.insert(i, stack.get())
		i += 1
	return q


def modifyQueue(q, k):
	s = []
	if len(q) == 0 or k == 0:
		return q
	for i in range(k):
		data = q.pop(0)
		s.append(data)
	for i in range(k):
		q.append(s.pop())
	longi = len(q) - k
	for i in range(longi):
		newdata = q.pop(0)
		q.append(newdata)
	return q