GeeksforGeeks » Multiple Choice Questions
Stack and permutation question
(4 posts)-
Stack A has the entries a,b,c ( with a on the TOp)Stack B is empty.
an entry pooped out of the stack A can be printed immediatly or pushed to stack B. An Entry popped out of the stack B can only be printed. In this Arrangement ,if Stack A has 4 entries, then number of possible permutation will be(a) 24 (b) 12 (c) 21 (d) 14
-
I think it will be (b) 12 becoz say stack A has 4 entries as a,b,c,d.Now a is always on top of the stack.So,rest 3 elements can be permuted in 3! =6 ways.Now if they are printed immediately then there are 6 ways..but if they are pushed on stack B and then printed,then there are another 6 ways.So,a total of 12 ways..
-
ans should be fact(4) because every possible combination or sequence is possible by using 2 stacks , so all possible combinations are fact(4).
-
Answer is Catalan number, option (d).
Case 1:
Assume that the stack A is having only one element, it can be pushed to B or printed, any how only one time it can be printed.Case 2:
Stack A has 2 elements (a, b). b is top element. b can be pushed to B or printed. The outcome can be (b, a) or (a, b).Case 3:
Stack A has 3 elements (a, b, c). c is on top of stack. The outcomes can be any of the following (total 5),Push a, b, c to B and then pop - c, b, a
Push c to B, print b, and push a to B, pop all elements of B - b, a, c
Push c to B, push b to B, print a, and pop all elements of B - a, b, c
Print c, push b, a to B, and pop all elements of B - c, a, b
Push c to B, Print b, and pop c from B, and Print a - b, c, aC(1) = 1, C(2) = 2, C(3) = 5 --- Catalan Series.
Case 4:
Stack A has 4 elements. We can generate C(4) = 15 permutations, hence the option should be (d).
Reply
You must log in to post.