VOOZH about

URL: https://www.geeksforgeeks.org/dsa/design-a-dynamic-stack-using-arrays-that-supports-getmin-in-o1-time-and-o1-extra-space/

⇱ Design a dynamic stack using arrays that supports getMin() in O(1) time and O(1) extra space - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Design a dynamic stack using arrays that supports getMin() in O(1) time and O(1) extra space

Last Updated : 23 Jul, 2025

Design a special dynamic Stack using an array that supports all the stack operations such as push(), pop(), peek(), isEmpty(), and getMin() operations in constant Time and Space complexities.

Examples:

Assuming the right to left orientation as the top to bottom orientation and performing the operations:

  1. Push(10): 10 is added to the top of the stack. Thereafter, the stack modifies to {10}.
  2. Push(4): 4 is added to the top of the stack. Thereafter, the stack modifies to {10, 4}.
  3. Push(9): 9 is added to the top of the stack. Thereafter, the stack modifies to {10, 4, 9}.
  4. Push(6): 6 is added to the top of the stack. Thereafter, the stack modifies to {10, 4, 9, 6}.
  5. Push(5): 5 is added to the top of the stack. Thereafter, the stack modifies to {10, 4, 9, 6, 5}.
  6. Peek(): Prints the top element of the stack 5.
  7. getMin(): Prints the minimum element of the stack 4.
  8. Pop(): Deletes the top most element, 5 from the stack. Thereafter, the stack modifies to {10, 4, 9, 6}.
  9. Pop(): Deletes the top most element, 6 from the stack. Thereafter, the stack modifies to {10, 4, 9}.
  10. Pop(): Deletes the top most element, 9 from the stack. Thereafter, the stack modifies to {10, 4}.
  11. Pop(): Deletes the top most element, 4 from the stack. Thereafter, the stack modifies to {10}.
  12. Peek(): Prints the top element of the stack 10.
  13. getMin(): Prints the minimum element of the stack 10.

Approach: To implement a dynamic stack using an array the idea is to double the size of the array every time the array gets full. Follow the steps below to solve the problem:

  • Initialize an array, say arr[] with an initial size 5, to implement the stack.
  • Also, initialize two variables, say top and minEle to store the index of the top element of the stack and minimum element of the stack.
  • Now, perform the following stack operations: 
    • isEmpty(): Checks if the stack is empty or not.
      • Return true if the top is less or equal to 0. Otherwise, return false.
    • Push(x): Inserts x at the top of the stack.
      • If the stack is empty, insert x into the stack and make minEle equal to x.
      • If the stack is not empty, compare x with minEle. Two cases arise:
        • If x is greater than or equal to minEle, simply insert x.
        • If x is less than minEle, insert (2*x – minEle) into the stack and make minEle equal to x.
      • If the array used is full then, double the size of the array and then copy all the elements of the previous array to the new array and then assign the address of the new array to the original array. Thereafter, perform the push operation as discussed above.
    • Pop(): Removes an element from the top of the stack.
      • Let the removed element be y. Two cases arise
      • If y is greater than or equal to minEle, the minimum element in the stack is still minEle.
      • If y is less than minEle, the minimum element now becomes (2*minEle – y), so update minEle as minEle = 2*minEle-y.
    • getMin(): Finds the minimum value of the stack.
      • If the stack is not empty then return the value of minEle. Otherwise, return "-1" and print "Underflow".

Illustration:

Push(x) 
 

👁 stack_insert
  • Number to be Inserted: 3, Stack is empty, so insert 3 into stack and minEle = 3.
  • Number to be Inserted: 5, Stack is not empty, 5> minEle, insert 5 into stack and minEle = 3.
  • Number to be Inserted: 2, Stack is not empty, 2< minEle, insert (2*2-3 = 1) into stack and minEle = 2.
  • Number to be Inserted: 1, Stack is not empty, 1< minEle, insert (2*1-2 = 0) into stack and minEle = 1.
  • Number to be Inserted: 1, Stack is not empty, 1 = minEle, insert 1 into stack and minEle = 1.
  • Number to be Inserted: -1, Stack is not empty, -1 < minEle, insert (2*-1 – 1 = -3) into stack and minEle = -1.

Pop() 
 

👁 stack_removal
  • Initially the minimum element minEle in the stack is -1.
  • Number removed: -3, Since -3 is less than the minimum element the original number being removed is minEle which is -1, and the new minEle = 2*-1 – (-3) = 1
  • Number removed: 1, 1 == minEle, so number removed is 1 and minEle is still equal to 1.
  • Number removed: 0, 0< minEle, original number is minEle which is 1 and new minEle = 2*1 – 0 = 2.
  • Number removed: 1, 1< minEle, original number is minEle which is 2 and new minEle = 2*2 – 1 = 3.
  • Number removed: 5, 5> minEle, original number is 5 and minEle is still 3

Below is the implementation of the above approach:

 
 


Output
Top Element : 5
Minimum Element : 4
Popped element : 5
Popped element : 6
Popped element : 9
Popped element : 4
Top Element : 10
Minimum Element : 10


 

Time Complexity: O(1) for each operation
Auxiliary Space: O(1)


 

Comment