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, where each input element is pushed in order and any top element may be popped at any time?

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

Correct answer: C. 42

A permutation is stack-sortable exactly when it avoids the pattern 231, and these are counted by the Catalan numbers (C5 = 42). Counting all 120 permutations ignores the stack restriction.

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

Exams that ask this topic