Which of the following permutation can be obtained in the same order using a…
2017
Which of the following permutation can be obtained in the same order using a stack assuming that input is the sequence 5, 6, 7, 8, 9 in that order?
- A.
7, 8, 9, 5, 6
- B.
5, 9, 6, 7, 8
- C.
7, 8, 9, 6, 5
- D.
9, 8, 7, 5, 6
Attempted by 522 students.
Show answer & explanation
Correct answer: C
To determine if a permutation is valid using a stack, simulate the push and pop operations. The input sequence 5, 6, 7, 8, 9 is pushed onto the stack in order. To obtain 7 first, push 5, 6, then 7, and pop 7. The stack contains [5, 6]. To obtain 8 next, push 8 and pop it. Then push 9 and pop it. The stack contains [5, 6]. Finally, pop 6 then 5. This sequence (7, 8, 9, 6, 5) is valid because it respects the Last-In-First-Out property. Any option requiring an element to be popped while a newer element remains below it in the stack is invalid.
A video solution is available for this question — log in and enroll to watch it.