in DS
13,216 views
9 votes
9 votes

Stack A has the entries a, b, c (with a on top). Stack B is empty. An entry popped out of stack A can be printed immediately or pushed to stack B. An entry popped out of the stack B can be only be printed. In this arrangement, which of the following permutations of a, b, c are not possible?

  1. b a c
  2. b c a
  3. c a b
  4. a b c
in DS
13.2k views

1 Answer

10 votes
10 votes
Best answer
ans is C  c a b is not possible
selected by

4 Comments

can you please explain it why it is so..?
0
0
initially stack A will have a b c with a on top and empty stack B now if we pop from stack A a will come out and then b and then c these a b c can either be printed or pushed in B and whatever we pop from b can only be printed now one possibility is a is popped and pushed in B then b is popped from A and printed then a is popped and printed and finally c is popped and printed this will give bac which is option A. second possibility is a is popped and pushed in B then b is popped from A and printed then c is popped from A and printed and a is popped from B and printed which gives bca (Option B) . another possible sequence is unstack A which gives a b c (Option D) Option C is not possible as to print c first a and b must be pushed into B and when we pop b will come out before a which will give c b a
0
0

komal see here

stack A
     a
     b
     c

 

stack B
 
 
 

for option a) sequence is...

POP A(stack)///////it will pop 'a' as it is on top

PUSH B(stack) /////// it will push 'a' into stack B

POP A  //////it will pop 'b' as it is on top

print .....it will print 'b'///////as POP from stack A can be printed immediately

POP B

print/////// it will print 'a'

POP A

print it will print 'c.....so we get 'bac'  as permuatation...

option (b)   similarly  POP A, PUSH B,POP A,print////b,POP A,print////c, POP B,print///a........bca

option(d) POP A,print///a, POP A,print///b, POP A,print///c.........abc printed

option(c)....this sequence is not possible...bcoz to print 'c' u need  to pop first 'a' then insert inside stack B and again..pop A...and insert inside STACK B..then u can print 'c'.. but...now u cannot..print 'a' ..coz 'b' is on top in STACK B..and in STACK B if u pop u have print it neccessarily...so 'b' has to printed..first..then only u can print 'a'....cab...not possible

5
5
must provide explanation....only answers are also available on other sites////////...selectors...must see this before selecting answers...
0
0