A stack permutation is a permutation of objects in the given input queue which is done by transferring elements from input queue to the output queue with the help of a stack and the built-in push and pop functions.
The well defined rules are:
- Only dequeue from the input queue.
- Use inbuilt push, pop functions in the single stack.
- Stack and input queue must be empty at the end.
- Only enqueue to the output queue.
There are a huge number of permutations possible using a stack for a single input queue.
Given two arrays, both of unique elements. One represents the input queue and the other represents the output queue. Our task is to check if the given output is possible through stack permutation.
Input : First array: 1, 2, 3 Second array: 2, 1, 3 Output : Yes Procedure: push 1 from input to stack push 2 from input to stack pop 2 from stack to output pop 1 from stack to output push 3 from input to stack pop 3 from stack to output Input : First array: 1, 2, 3 Second array: 3, 1, 2 Output : Not Possible
The idea to do this is we will try to convert the input queue to output queue using a stack, if we are able to do so then the queue is permutable otherwise not.
Below is the step by step algorithm to do this:
- Continuously pop elements from the input queue and check if it is equal to the top of output queue or not, if it is not equal to the top of output queue then we will push the element to stack.
- Once we find an element in input queue such the top of input queue is equal to top of output queue, we will pop a single element from both input and output queues, and compare the top of stack and top of output queue now. If top of both stack and output queue are equal then pop element from both stack and output queue. If not equal, go to step 1.
- Repeat above two steps until the input queue becomes empty. At the end if both of the input queue and stack are empty then the input queue is permutable otherwise not.
Below is implementation of above idea:
This article is contributed by Suprotik Dey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if two arrays are permutations of each other
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Check if an array can be formed by merging 2 non-empty permutations
- Check if a binary string contains all permutations of length k
- Check if the given array is same as its inverse permutation
- Iterative approach to print all permutations of an Array
- Find the Number of Permutations that satisfy the given condition in an array
- Print k different sorted permutations of a given array
- Count permutations of given array that generates the same Binary Search Tree (BST)
- Count possible permutations of given array satisfying the given conditions
- Stack | Set 3 (Reverse a string using stack)
- Sort a stack using a temporary stack
- Infix to Postfix using different Precedence Values for In-Stack and Out-Stack
- Find maximum in stack in O(1) without using additional stack
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- Check if the given permutation is a valid BFS of a given Tree
- Check if permutaion of one string can break permutation of another
- Write a program to print all permutations of a given string
- Print all permutations with repetition of characters
- Print all permutations in sorted (lexicographic) order