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?
- A. 120
- B. 32
- C. 42
- 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.
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 Josephus game, 41 people stand in a circle numbered 1 to 41. Starting from person 1, every 3rd person is removed (counting…
- What is the area of the largest rectangle that fits in a histogram whose bar heights are 2, 1, 5, 6, 2, 3 (each bar has width 1)?
- Rain falls on the elevation map [4, 2, 0, 3, 2, 5] (each bar has width 1). How many units of water are trapped in total?
- How many contiguous subarrays of [1, 2, 1, 2, 3, 3, 4] contain at most 2 distinct values?
- How many non-empty contiguous subarrays of [10, 5, 2, 6] have a product strictly less than 100?
- An LRU cache holds 3 keys. Requests arrive in the order 1, 2, 3, 1, 4, 2, 5, 1, 2, 3, 4, 5, and a request for an absent key is a miss that…