Stack M has the entries 1,2,3(with a on top).Stack N is empty.An entry popped out of stack M can be printed immediately or pushed to stack N.An entry popped out of the stack N can only be printed. In this arrangement, which of the following permutations of 1,2,3 are not possible?Options3,1,21,2,32,1,33,2,1
Question
Stack M has the entries 1,2,3(with a on top).Stack N is empty.An entry popped out of stack M can be printed immediately or pushed to stack N.An entry popped out of the stack N can only be printed. In this arrangement, which of the following permutations of 1,2,3 are not possible?Options3,1,21,2,32,1,33,2,1
Solution
The permutation that is not possible is 2,1,3.
Here's why:
- For 3,1,2: Pop 3 from stack M and print, pop 2 and push it to stack N, pop 1 from stack M and print, finally pop 2 from stack N and print. So, the sequence is 3,1,2.
- For 1,2,3: Pop 3 from stack M and push it to stack N, do the same for 2, then pop 1 from stack M and print, then pop 2 and 3 from stack N and print. So, the sequence is 1,2,3.
- For 3,2,1: Pop 3 from stack M and print, pop 2 and print, finally pop 1 and print. So, the sequence is 3,2,1.
- For 2,1,3: There's no way to get this sequence. If you pop 3 and 2 from stack M and push them to stack N, you can print 1, but then you can only print 2 and 3 in the order 3,2 (since 3 is now on top of stack N), not 2,3. If you print 2 immediately after popping it from stack M, you can't get 1 out before 3.
Similar Questions
Stack A has the entries a, b, c (with a on top). Stack B is empty. An entry popped out of stack A can be printed immediately or pushed to stack B. An entry popped out of the stack B can be only be printed. In this arrangement, which of the following permutations of a, b, c are not possible?
Select the correct answerStack M has the entries 1,2,3(with a on top).Stack N is empty.An entry popped out of stack M can be printed immediately or pushed to stack N.An entry popped out of the stack N can only be printed. In this arrangement, which of the following permutations of 1,2,3 are not possible?Options1,2,32,1,33,2,13,1,2
Which of the following permutations can be obtained in the output(in the same order),using a stack assuming that the input is the sequence A,B,C,D,E in that order?OptionsC,D,E,A,BC,D,E,B,AC,D,A,B,EC,D,A,E,B
How many numbers will the stack contain after the following operations on a stack of size 7?Push(8);Push(0);Pop();Push(2);Push(3);Pop();Push(4);Pop();Pop();Pop();Push(5);a) 1b) 2c) 3d) 4
Consider the following operation performed on a stack of size 5.Push(1);Pop();Push(2);Push(3);Pop();Push(4);Pop();Pop();Push(5);After the completion of all operation, the number of elements present in stack is?1 234Clear ResponseSave & Next
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.