GATE Computer ScienceVery hardGATE CS
Items 1, 2, 3, 4, 5 arrive in this order and are inserted, each at either the front or the back of a double-ended queue (the choice is free); at any time the item at the front or at the back may be removed and output. How many of the 120 permutations of 1 to 5 can be output?
- A. 90
- B. 120
- C. 116
- D. 114
Correct answer: C. 116
Enumerating all insert-front/insert-back and remove-front/remove-back sequences gives 116 distinct output permutations for n = 5 (and 24 for n = 4, 634 for n = 6). Only four permutations of 5 items are impossible, in contrast with a single stack which allows only 42.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- A tree has 16 nodes numbered 0 to 15 with the parent array parent[1..15] = 0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 9, 9, 10 (node i has parent…
- The same 16-node tree (parent array 0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 9, 9, 10 for nodes 1 to 15, root 0) is to be cut by deleting edges…
- A binary tree is given in preorder with # for an empty child: 1 2 4 8 # # 9 # # 5 # 10 # # 3 6 # 11 # # 7 12 # # 13 # #. What is the…
- An undirected graph on vertices 0 to 7 has the edges 0-1, 0-2, 0-3, 1-2, 1-4, 2-3, 2-5, 3-4, 3-5, 4-5, 4-6, 5-6, 5-7, 6-7. Let A be its…
- An m × n sparse matrix with m = 1000 rows is stored in coordinate form (three arrays row, col, val, each of length nnz) or in compressed…
- Bubble sort with early exit (the algorithm stops after a pass in which no swap occurs) sorts the array [6, 9, 3, 8, 0, 5, 2, 7, 1, 4] into…