Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Anish and Stacks!

Moderate
0/80
Average time to solve is 30m
profile
Contributed by
9 upvotes
Asked in company
Microsoft

Problem statement

Anish has become very attentive in his data structures classes. Now his teacher has given the assignment to implement N stacks in a single array of length L. Since he has been very inattentive in his data structure classes, he can’t solve the problem on his own. Can you help him out?

Example:-
Let, 

N = 5
L = 10
1 1 3 [PUSH (1 ,4)]
1 1 4 [PUSH (1 ,3)] 
2 1 [POP(1)] 
2 2 2 [PUSH(2,2)] 
2 2 [POP(2)]

Answer:- [0, 0, 4, 0, 2]
 (The first stack has 4 and 3 so 4 is popped out from stack 1 and stack 2 has integer 2 in it, so 2 is popped out from stack 2) .
Detailed explanation ( Input/output format, Notes, Images )
Constraints :
1 <= T <= 5
1 <= L <= 10^5
1 <= N <= 10^5
1 <= Q <= 10^5
1 <= A <= 2
1 <= B <= N
1 <= C <= 10^9  

Time Limit: 1 sec
Sample Input 1 :
1
10 1 5
1 1 1
1 1 3
2 1
2 1
2 1
Sample Output 1 :
0
0
3
1
-1 
Explanation for Sample Output 1 :
In the first two operations, the answer should be 0 because the push operations are performed successfully. Then for the next two operations, the numbers 3 and 1 are popped out and they are printed. In the last operation, -1 is printed because the stack is already empty.
Sample Input 2 :
1
10 2 1
2 2 
Sample Output 2 :
-1
Full screen
Console