in Programming in C
1,545 views
4 votes
4 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 ?
in Programming in C
1.5k views

1 Answer

3 votes
3 votes

Case 1:
Push 1, pop 1
push 2, pop 2
push 3, pop 3

Case 2:
push 1
Push 2, Pop 2
Pop 1
push 3, pop 3


As we can see there 2 possibilities before 3(1, 2 and 2, 1), and there will be 4 possibilities after 3  (456, 546, and 564, 654,465).
so far we got 10 sequences.
Remaining 16 will be as follows:

4 5 3 2 1 6
4 5 3 2 6 1
4 5 3 6 2 1
5 4 3 2 6 1
5 4 3 6 2 1
5 4 3 2 1 6

143256

143265

143526

143562

143652

243156

243165

243516

243561

243651

Hence, there total 26 sequences possible.

edited

4 Comments

@A_I_$_h  i think you are looking this question as a permutation combination question in which 3 is fixed and 5 slots are empty and we have 5 choices to fill it hence 5!(5 factorial) but stack property should be preserved in this question

for Example:

in your 5! there must be a string 6 5 3 2 1 4 which is not possible in stack so i think your approach don't fit here

correct me if i am wrong
–1
–1
but given answer is 120
0
0

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

0
0

Related questions