Data Structures & AlgorithmsVery hardGATE CS · TCS NQT
Exactly how many permutations of 1..5 can be sorted by a single stack (push and pop operations, output must be 1, 2, 3, 4, 5)?
- A. 120
- B. 42
- C. 32
- D. 60
Correct answer: B. 42
Stack-sortable permutations of length n are counted by the Catalan number C_n; C_5 = 42. Counting all 5! = 120 permutations ignores the stack constraint.
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
- A queue holds 3, 1, 4, 2, 5 from front to back. You repeatedly look at the front element: if it is the next value needed (starting with…
- For windows of size 3 over [3, 1, 4, 2, 5, 1, 6, 0], how many windows have their maximum at the last position of the window (ties count as…
- Four unit-time jobs given as (deadline, profit) are (4, 20), (1, 10), (1, 40), (1, 30). Each job must finish by its deadline and only one…
- What is the length of the shortest contiguous subarray which, if sorted in place, makes the whole array [2, 6, 4, 8, 10, 9, 15] sorted?
- What is the number of trailing zeros in the decimal representation of the product 10 25 4 15 50?
- How many distinct sums appear over all non-empty contiguous subarrays of [2, 1, 3, 1]?