Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div2 D

Problem: The permutation [2, 1, 4, 3] is processed by a single stack: input elements are pushed in order and popped whenever the popped element can be output next in sorted order (1, 2, 3, ...). Is the permutation sortable by this stack? Answer 1 for yes and 0 for no.

  1. A. 1
  2. B. 2
  3. C. 0
  4. D. 3

Correct answer: A. 1

A single stack sorts a permutation exactly when the greedy pop-when-next simulation empties the stack. Here 2 is pushed and 1 pops, so the stack handles both pairs and the permutation is sortable.

More Coding Interview Problems questions

Exams that ask this topic