73 views

Suppose a queue $Q$ and two stacks $S_{1}$ and $S_{2}$ as given below.

void enqueue(Q,x){
push(S1,x);
}
void dequeue(Q,x){
if(stack-empty(S2))then
if(stack-empty(S1))then{
print("Q is empty");
return;
}
else while(!stack-empty(S1)){
x=pop(S1);
push(S2,x);
}
x=pop(S2);
}

The number of push and pop operation needed is represented by $X$ and $Y$, Then $X+Y$ is equal to______________

$Enqueue(4),Enqueue(3),Enqueue(2),Dequeue, Enqueue(6),Dequeue,Dequeue, Dequeue,Enqueue(5)$

Please tell value of X and Y are u getting

edited | 73 views

+1 vote
I think the ans. should be 17 because for every enqueue opeation there is only one stack operation (simply push in S1 ) performed .

but in case of dequeue  we required 3 stack operation

1. POP from stack1
2. Push in  stack2
3. Then POP from stack2

Here we have 5 enqueue operation and 4 dequeue opeation

Therefore X=9  ( 5 push for enqueue and 4 push for dequeue) And     Y=8 (2 times POP for dequeue)
by (403 points)
selected by
0
yes, we can say

$4$ elements push in 1st stack

pop from 1st stack.

then push in 2nd stack

and pop from 2nd stack

Total $8$ push and $8$ pop.operation

Now, last element only push in 1st stack.