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?

  1. A. 90
  2. B. 120
  3. C. 116
  4. 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.

More Programming and Data Structures (GATE) questions

Exams that ask this topic