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)?
- A. 42
- B. 120
- C. 32
- 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.
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
- Repeatedly delete any two adjacent equal letters in "abbacddcaeeaf" until no such pair remains. What is the length of the final string?
- What is the length of the longest well-formed (valid) parentheses substring of "()(()())))(()"?
- What is the minimum number of parentheses that must be ADDED (at any positions) to make "())(()))((" a valid parentheses string?
- For daily temperatures [73, 74, 75, 71, 69, 72, 76, 73], the wait for day i is the number of days until a strictly warmer day (0 if none)…
- Four processes need CPU bursts [5, 3, 8, 2] (all arrive at time 0). A round-robin scheduler gives each process up to a quantum of 2 units…
- Among all non-empty contiguous subarrays of 2 -1 4 -3 5 1, what is the largest sum that does NOT exceed 6?