edited by
3,386 views
2 votes
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
edited by

1 Answer

2 votes
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.

Related questions

3 votes
3 votes
0 answers
1
h4kr asked Jan 31, 2023
617 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
4 votes
4 votes
2 answers
2
srestha asked Jan 4, 2017
7,608 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)...
2 votes
2 votes
2 answers
3
1 votes
1 votes
2 answers
4
Shamim Ahmed asked Dec 11, 2018
723 views
Every recursive program uses strictly more stack space compared to its iterative equivalent.This statement is false. Please explain with examples