Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

How many permutations of {1, 2, 3, 4, 5} can be sorted into increasing order by a single stack, when the input is read left to right and each element is either pushed or popped (with output in increasing order)?

  1. A. 42
  2. B. 120
  3. C. 32
  4. D. 44

Correct answer: A. 42

A permutation is stack-sortable exactly when it avoids the pattern 231, and the count of such permutations is the Catalan number C₅ = 42. The naive total 5! counts every input order, and 2⁵ counts subsets, not orders.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic