798 views
1 votes
1 votes

How many permutations can be obtained in the output using a stack assuming that the input 1,2,3,4,5,6 such that 3 will be popped out from stack at 3rd position ?

3 will be popped out from stack at 3rd position

 what does this mean?? 

1) third pop operation performed should be of 3

2)3 is at third position while pushing into the stack

really confused , pls help

1 Answer

Best answer
1 votes
1 votes

3 should be at third pop, before that we have to do two pop operations.

I) fix the first pop operation as 1, then

            i) fix the second pop operation as 2, ===> remaining three elements i.e., 4,5 and 6 lead to $\frac{\binom{2n}{n}}{n+1} = \frac{\binom{6}{3}}{4} = 5 $

            ii) fix the second pop operation as 4, ===> remaining three elements i.e., 5,6, and 2

                  5 and 6 yet to be pushed but remember 2 is in stack ===> lead to $\frac{\binom{2n}{n}}{n+1} = \frac{\binom{6}{3}}{4} = 5 $

 

           iii) fix the second pop operation as 5, ===> if 2$^{nd}$ pop is 5, then third pop should be 4 ===> invalid case

                  WHY ?

                     4 is pushed after 3, 5 is pushed after 4 ===> 4 is lie between 3 and 5, then without poping 4, you can't pop 3.

           iv) fix the second pop operation as 6, ===> if 2$^{nd}$ pop is 6, then third pop should be 5 ===> invalid case

 

II) fix the first pop operation as 2, then

            i) fix the second pop operation as 1, ===> remaining three elements i.e., 4,5 and 6 lead to $\frac{\binom{2n}{n}}{n+1} = \frac{\binom{6}{3}}{4} = 5 $

            ii) fix the second pop operation as 4, ===> remaining three elements i.e., 5,6, and 1

                  5 and 6 yet to be pushed but remember 1 is in stack ===> lead to $\frac{\binom{2n}{n}}{n+1} = \frac{\binom{6}{3}}{4} = 5 $

 

           iii) fix the second pop operation as 5, ===> if 2$^{nd}$ pop is 5, then third pop should be 4 ===> invalid case

                  WHY ?

                     4 is pushed after 3, 5 is pushed after 4 ===> 4 is lie between 3 and 5, then without poping 4, you can't pop 3.

           iv) fix the second pop operation as 6, ===> if 2$^{nd}$ pop is 6, then third pop should be 5 ===> invalid case

 

III) fix the first pop operation as 4, then

            i) fix the second pop operation as 5, ===> remaining three elements i.e., 1,2 are in stack and 6 is yet to be pushed

                     then possible, 1 should be after 2 and 6 can be any where ===> 3 orders

                     i.e., 2 1 6  ,  2 6 1  and  6 2 1

          iv) fix the second pop operation as 6, ===> if 2$^{nd}$ pop is 6, then third pop should be 5 ===> invalid case

   Note you can't pop either 2 or 1 as second pop, due to 3 is pushed on those 2 elements

 

IV) fix the first pop operation as 5, then second pop operation should be 4, to get third pop operation is 3

            ===> remaining three elements i.e., 1,2 are in stack and 6 is yet to be pushed

                     then possible, 1 should be after 2 and 6 can be any where ===> 3 orders

                     i.e., 2 1 6  ,  2 6 1  and  6 2 1

Total = I + II + III + IV = ( 5+5 = 10 ) + ( 5+5 = 10 ) + 3 + 3 = 26

selected by

Related questions

0 votes
0 votes
0 answers
1
Gate Fever asked Jan 12, 2019
792 views
consider the following statement:-data link layer define the boundaries of frame because framing is always of fixed sizetrue or false??acc. to me , its true
0 votes
0 votes
0 answers
2
Gate Fever asked Jan 2, 2019
334 views
number of ways possible to form injective function from set A to setB where |A|=3 and |B|=5 where the pth element of set A cannot match with pth element of set B?
0 votes
0 votes
0 answers
3
Gate Fever asked Dec 25, 2018
916 views
The number of positive number which divides either 2700 or 9000 are _________? i am getting 20
0 votes
0 votes
1 answer
4
Gate Fever asked Dec 16, 2018
1,145 views
The median of n elements can be found in O(logn) time.which one of the following is correct about worst case time complexity of quick sort,if always median is selected a...