Skip to content

Stacks perform getmin in O(1) operation

Devrath edited this page Jul 27, 2021 · 3 revisions

PROBLEM STATEMENT: We are having a stack of integers, Now we need to have a function that returns the minimum value in the stack and this should hold in case of push and pop operations are done

Use case:

  • Assume there are 4 elements in stack 8-10-3-7.
  • Now if you want to get the minimum element now ----> Here the output is 3.
  • Now say you continuously pop 2 more times meaning, you remove 3 and 7, Here when you execute minimum element ---> Result is 8.
  • Same way if you insert one new element(1) now to remaining 8-10, then the on executing minimum element ---> Result is 1.

Algorithm:

  • Initial state on input in stack 8-10-3-7.
Clone this wiki locally