256 views
2 votes
2 votes

Stack A has entries a,b,c,d(with a on top).Stack B is empty.

An entry popped out of stack A is pushed into stack B.

An entry popped out of stack B can only be printed.

The no. of possible permutation for printing the elements are __ ?

  1. 24
  2. 12
  3. 21
  4. 14

can anybody give a generalized formula with explanation for this ?

1 Answer

Best answer
1 votes
1 votes

This problem can be interpreted as

how many distinct arrangements of n pairs of left-right parentheses are there all of which close?

how? consider push(a)  to be first opening opening parenthesis and pop(a) to be first closing parenthesis

similarly push(b) to be first second opening parenthesis and pop(b) to be second closing parenthesis

similarly for c and d

Answer to this question is nth catalan number

$\large C_n = \frac{1}{n+1} \binom{2n}{n}$

In our question n= 4,

$\large C_n = \frac{1}{4+1} \binom{8}{4} = \frac{1}{5} \times 70 = \color{red}{14}$

selected by

Related questions

0 votes
0 votes
1 answer
1
tishhaagrawal asked Dec 16, 2023
360 views
Below is my approach to solving this question, can anyone please explain if I am doing it the right way?Let X = #free slotssince, m =7 and n = 3So, $4 \leqslant x\leqsla...
0 votes
0 votes
0 answers
2
Sgm asked Dec 28, 2022
387 views
Any one please let me know if there is any other way to this output?
0 votes
0 votes
0 answers
3
Pranavpurkar asked Sep 27, 2022
595 views
DOUBT 1: if head = P → link.is performed then what will happen to the nodes containing values a and b? will they get removed as no link is pointing them?and we will ...