• edited by
4,530 views
2 2 votes
Please  solve this question

Q   Assume stack A has the entries p, q and r (with p on top and r on bottom). Initially stack B is empty. An entry popped out of stack A can be printed immediately or pushed to stack B. A entry popped out of stack B can only be printed.
The least number of stack permutations of input sequence that start with a particular letter

1 Answer

2 2 votes

As in above question stack A has either print element or push on it B. the stack B has must print the element;

The element is respectively p,q,r so total 8 combinations are possible such as:

POSSIBLE WAYS TO ARRANGE P,Q,R
1 P Q R
2 P R Q
3 Q P R
4 Q R Q
5 R P Q
6 R Q P

first choice p,q,r: print p,q,r from stack A get pqr as output.

second choice p,r,q: print p from stack A, pop q and push it on stack B, print r and then print q from B getting p,r,q as output.

third choice q,p,r: pop p  from stack A and push to stack B.print q, then print p and last print r.

fourth choice q,r,p: pop p from stack A and push to stack B. print q then r and last print p.

fifth choice r,p,q: pop p,q and push it to stack b. print r but in stack B q is the topmost element but we required p as output so this combination is not possible.

sixth choice r,q,p: pop p,q form stack A and push it to B. print r then print topmost element of B that is q,p respectively.

so out of sixth combination 5 are valid and only one combination (r,p,q) is not valid.

Position:
Show:

Related questions

1 1 vote
1 1 answer
1.7k
1.7k views
h4kr asked Jan 31, 2023
1,681 views
Number of possible permutations that can be obtained using stack if the input sequence is 1, 2, 3, 4, 5 (in the order) is
6 6 votes
2 answers 2 answers
9.8k
9.8k views
srestha asked Jan 4, 2017
9,845 views
Which of the following permutations can be obtained in the output (in the same order) using a stack assuming that the input is the sequence 5, 7, 8, 4, 6 in that order?a)...
0 0 votes
2 2 answers
529
529 views
2 2 votes
2 2 answers
2.4k
2.4k views
Gurdeep Saini asked Jan 2, 2019
2,391 views
true/false ?) if stack is implemented as a array,all operation push ,pop ,is emptystack(),delete stack() can be performed in constant time.)if stack is implemented as a l...