Given a stack of integers. The task is to design a special stack such that maximum element can be found in O(1) time and O(1) extra space.
Given Stack : 2 5 1 64 --> Maximum So Output must be 64 when getMax() is called.
Below are the different functions designed to push and pop elements from the stack.
Push(x) : Inserts x at the top of stack.
- If stack is empty, insert x into the stack and make maxEle equal to x.
- If stack is not empty, compare x with maxEle. Two cases arise:
- If x is less than or equal to maxEle, simply insert x.
- If x is greater than maxEle, insert (2*x – maxEle) into the stack and make maxEle equal to x. For example, let previous maxEle was 3. Now we want to insert 4. We update maxEle as 4 and insert 2*4 – 3 = 5 into the stack.
Pop() : Removes an element from top of stack.
- Remove element from top. Let the removed element be y. Two cases arise:
- If y is less than or equal to maxEle, the maximum element in the stack is still maxEle.
- If y is greater than maxEle, the maximum element now becomes (2*maxEle – y), so update (maxEle = 2*maxEle – y). This is where we retrieve previous maximum from current maximum and its value in stack. For example, let the element to be removed be 5 and maxEle be 4. We remove 5 and update maxEle as 2*4 – 5 = 3.
- Stack doesn’t hold actual value of an element if it is maximum so far.
- Actual maximum element is always stored in maxEle
- Number to be Inserted: 3, Stack is empty, so insert 3 into stack and maxEle = 3.
- Number to be Inserted: 5, Stack is not empty, 5> maxEle, insert (2*5-3) into stack and maxEle = 5.
- Number to be Inserted: 2, Stack is not empty, 2< maxEle, insert 2 into stack and maxEle = 5.
- Number to be Inserted: 1, Stack is not empty, 1< maxEle, insert 1 into stack and maxEle = 5.
- Initially the maximum element maxEle in the stack is 5.
- Number removed: 1, Since 1 is less than maxEle just pop 1. maxEle=5.
- Number removed: 2, 2<maxEle, so number removed is 2 and maxEle is still equal to 5.
- Number removed: 7, 7> maxEle, original number is maxEle which is 5 and new maxEle = 2*5 – 7 = 3.
Number Inserted: 3 Number Inserted: 5 Maximum Element in the stack is: 5 Number Inserted: 7 Number Inserted: 19 Maximum Element in the stack is: 19 Top Most Element Removed: 19 Maximum Element in the stack is: 7 Top Most Element Removed: 7 Top Most Element is: 5
- Design a stack that supports getMin() in O(1) time and O(1) extra space
- Reverse a stack without using extra space in O(n)
- Clone a stack without extra space
- Design a stack to retrieve original elements and return the minimum element in O(1) time and O(1) space
- Find duplicate in an array in O(n) and by using O(1) extra space
- Sliding Window Maximum (Maximum of all subarrays of size k) using stack in O(n) time
- Find maximum in stack in O(1) without using additional stack
- Sum of K largest elements in BST using O(1) Extra space
- Merge two BSTs with constant extra space
- Find maximum and minimum element in binary tree without using recursion or stack or queue
- Design and Implement Special Stack Data Structure | Added Space Optimized Version
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space
- Maximum removal from array when removal time >= waiting time
- Tracking current Maximum Element in a Stack
- Design a stack which can give maximum frequency element
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.