How can you implement a stack such that you can retrieve the minimum element in constant time?

  • It's not possible
  • Using a linked list
  • Using a priority queue
  • Using an additional stack
You can implement a stack that allows retrieving the minimum element in constant time by using an additional stack to keep track of the minimum values. Whenever you push an element onto the main stack, you compare it with the top element of the auxiliary stack and push the smaller of the two. This ensures constant-time retrieval of the minimum element.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *