Stack A has the entries a, b, and c (with a on top). Stack B is empty....
Introduction
This question is related to the permutation of three elements (a, b, and c) in two stacks A and B. We need to determine which permutation is not possible.
Permutations
There are six possible permutations of three elements (a, b, and c):
Solution
We can solve this problem by analyzing each permutation and checking if it is possible or not. We will use the following notations for the stacks:
- A: original stack with entries a, b, and c
- B: empty stack
We will also use the following operations:
- pop(A): remove the top element from stack A
- pop(B): remove the top element from stack B
- push(B, x): push element x onto stack B
- print(x): print element x
abc
We can print a, then push b to B, then push c to B, and finally pop and print elements from B.
pop(A) -> print(a)
pop(A) -> push(B, b)
pop(A) -> push(B, c)
pop(B) -> print(c)
pop(B) -> print(b)
Therefore, permutation abc is possible.
acb
We can print a, then push c to B, then push b to B, and finally pop and print elements from B.
pop(A) -> print(a)
pop(A) -> push(B, c)
pop(A) -> push(B, b)
pop(B) -> print(b)
pop(B) -> print(c)
Therefore, permutation acb is possible.
bac
We can push b to B, then print a, then push c to B, and finally pop and print elements from B.
pop(A) -> push(B, b)
pop(A) -> print(a)
pop(A) -> push(B, c)
pop(B) -> print(c)
pop(B) -> print(b)
Therefore, permutation bac is possible.
bca
We can push b to B, then push c to B, then print a, and finally pop and print elements from B.
pop(A) -> push(B, b)
pop(A) -> push(B, c)
pop(A) -> print(a)
pop(B) -> print(c)
pop(B) -> print(b)
Therefore, permutation bca is possible.
cab
We can print a, then push b to B, then print c, and finally pop and print elements from B.
pop(A) -> print(a)
pop(A) -> push(B, b)
pop(A) -> print(c)
pop(B) -> print(b)