Data Structures & AlgorithmsVery hard
A linear-time check decides whether a permutation can be sorted by one stack. Which property is equivalent to being stack-sortable?
- A. The permutation has no pattern 231 (no i < j < k with a[k] < a[i] < a[j]).
- B. The permutation has no two adjacent descents.
- C. The permutation's first element is 1.
- D. The permutation is an involution.
Correct answer: A. The permutation has no pattern 231 (no i < j < k with a[k] < a[i] < a[j]).
Stack-sortable permutations are exactly the 231-avoiding ones, which is a classic characterization. The other properties are either too weak or unrelated.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- In the jump game (minimum number of jumps), why does a single left-to-right scan with a 'current boundary' count the jumps correctly?
- In the 'first non-repeating character in a stream' problem, why is a queue with lazy removal used?
- Why is a hash map (not a sorted array) used to find the longest run of consecutive integers in an unsorted array in linear expected time?
- When tracking the first occurrence of every value for an order-preserving de-duplication, which structure gives expected O(1) membership…
- A subarray's average is at least K exactly when which quantity is non-negative for that subarray?
- Convert the infix expression a+b(c-d)/e to postfix with the shunting-yard algorithm (operators '+', '-', '', '/' with '' and '/' binding…