@balchandar reddy san
what would be the answer if we inserted the elements in ascending order i.e.. 1,2,3,4,5,6 order ?
gauravky
cant it occur in the way that I just mentioned in the previous comment?
gauravkc
I was speaking about this question..
@Somoshree Datta 5 Even considering your case, there will be 1 more possibility as you can push 1,2,3,4 in order and then pop 4 then add 5 and then pop 5 then you can pop 3.
Then it depends I think. The qn states elements are already in stack. If the stack has 1,2,3,4,5,6 then 0 ways as 3rd element will be 4. But if the stack is empty and we are free to perform push pops in any order, then 1 way as you said.
Woah! Didn't think of that @balchandar reddy san
balchandar reddy san
yes,so there are 2 permutations possible right?
where is it said that elements are already present in the stack?
@Somoshree Datta 5
Yes 2
push 1 , 2
pop 2 ..
then push 3 , 4
pop 4
pop 3 . 3 is popped at third position
@gauravkc
there can be many sequence like that for somoshee question... but the answer to the actual question is 5! right
https://math.stackexchange.com/questions/17769/using-one-stack-to-find-number-of-permutations
We can did such types of questions by Catalan number
But it's given that
3 will poped out from stack at 3rd position
Therefore 3rd position is fixed and we arranged the 5 numbers
By 2n Cn / (n+1) where n = 5
@Magma
My approch, not sure if its correct.
so given we need to pop element '3' in the 3rd position.
_ _ 3 _ _ _
the only possible numbers in the first 2 slots is 1,2 or 2,1 or 4,5 or 5,4.
consider the case where its 1,2
so we have 1 2 3 _ _ _
the 3 blank spaces can be filled with
4,5,6
4,6,5
5,4,6
5,6,4
6,5,4
the same possibilities are possible for 2,1 aswell.
now consider the case where 4,5 is filled in the first 2 blank spaces.
4 5 3 _ _ _
the leftover 3 blank space can be filled in
2,1,6
6,2,1
the same possibilities are possible for 5,4 aswell.
hence total possibilities are 5+5+2+2 = 14.
But why @pream sagar is not telling that that's the answer given ??
@OneZero
in the question given elements can be inserted in any order , but u r taking it only in increasing order.
if elements are inserted in any order then it will be 5!
i asked this question before, this answer is for the above question.
take this case push 1 , pop 1
push 2 , push 3 , push 4 .. pop 4 , pop 3
so first two positions are 14
64.3k questions
77.9k answers
244k comments
80.0k users