

1 ‘X’ N: Enqueue element ‘X’ into the end of the nth queue. Returns true if the element is enqueued, otherwise false.
2 N: Dequeue the element at the front of the nth queue. Returns -1 if the queue is empty, otherwise, returns the dequeued element.
Please note that Enqueue means adding an element to the end of the queue, while Dequeue means removing the element from the front of the queue.
The first line of input contains three space-separated integers ‘N’, ‘S’ and ‘Q’ denoting the number of queues, the size of the array and number of queries, respectively.
The next ‘Q’ lines specify the type of operation/query to be performed on the data structure.
Each query contains an integer ‘P’ denoting the type of query.
For query of type 1, the integer ‘P’ is equal to 1 and it is followed by two space-separated integers ‘X’ and ‘M’ denoting the element and queue on which operation is to be performed, respectively.
For query of type 2, the integer ‘P’ is equal to 2 and it is followed by a single integer ‘M’ denoting the queue on which operation is to be performed.
For each query, return the output returned after performing the corresponding operation on the data structure.
Print the output of each test case in a separate line.
You don’t need to print anything, It has already been taken care of. You Just have to complete the given functions.
1 <= N <= S <= 1000
1 <= Q <= 10^5
1 <= P <= 2
1 <= X <= 10^5
Time limit: 1 sec
One approach that you might be thinking of is to divide the complete array into ‘N’ parts. Like if there are 3 queues that you need to implement in a single array of size 12, then you divide the array into ‘S’ / ‘N’ = 12 / 3 = 4 parts of equal size and then implement a single queue in different arrays. But, this approach will not be valid for all cases, that is it will give ‘overflow’ and ‘underflow’ conditions even if it is possible to solve those conditions.
‘N’ = 3
‘S’ = 12
‘QUERIES’: 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8
Now, in this case, we divide the array into 3 parts of size = 4 each. Now if we enqueue elements 4 5 6 7 into the first queue and then try to enqueue 8 into the same, the array will show filled and the problem will return the ‘Overflow’ condition even when the array still has empty spaces where the element could be enqueued.
This makes this approach limited.
So, now we will work on a better approach that would solve this problem.
In this approach, we will use three arrays to store different indexes, which will help us to implement N queues into a single main array. Let us call these three arrays as start[], end[], next[].
The start array will store the indexes of the front elements of all the queues. The end array will store the indexes of the rear elements of all the queues. And finally, the next array will store the indexes of the next value for all values of the main array.
Apart from these, we will also initialize a stack that will store the indexes of the empty slots in the main array. Empty slots are the slots that are not yet filled. We will also store the top of this stack as a variable let’s say ‘TEMP’.
Now, after initializing all these data structures required, we move on to the algorithm part.
We need to complete two functions enqueue() and dequeue():