Computer Science, asked by akashdp50, 9 months ago

If the sequence of operations- pop, push (1), push (2), pop, pop, pop, push
(2), pop, are performed on a stack, find the sequence of popped out values.

Answers

Answered by Aryansingh001
6

Answer:

Input: pushed = { 1, 2, 3, 4, 5 }, popped = { 4, 5, 3, 2, 1 } Output: True Following sequence can be performed: push(1), push(2), push(3), push(4), pop() -> 4, push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1 Input: pushed = { 1, 2, 3, 4, 5 }, popped = { 4, 3, 5, 1, 2 } Output: False 1 can't be popped before 2.

Explanation:

: If the element X has been pushed to the stack then check if the top element in the pop[] sequence is X or not. If it is X then pop it right now else top of the push[] sequence will be changed and make the sequences invalid. So, similarly do the same for all the elements and check if the stack is empty or not in the last. If empty then print True else print False.

⬆️⬆️⤴️ hope it helps you

Answered by aryahikaul501
1

Answer:

Input: pushed = { 1, 2, 3, 4, 5 }, popped = { 4, 5, 3, 2, 1 } Output: True Following sequence can be performed: push(1), push(2), push(3), push(4), pop() -> 4, push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1 Input: pushed = { 1, 2, 3, 4, 5 }, popped = { 4, 3, 5, 1, 2 } Output: False 1 can't be popped before 2.

Explanation:

: If the element X has been pushed to the stack then check if the top element in the pop[] sequence is X or not. If it is X then pop it right now else top of the push[] sequence will be changed and make the sequences invalid. So, similarly, do the same for all the elements and check if the stack is empty or not in the last. If empty then print True else print False.

Mark me as brainliest.

Similar questions