Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8594537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:13:21+00:00 2026-06-12T00:13:21+00:00

How would you maintain a stack so that whenever you pop from the stack,

  • 0

How would you maintain a stack so that whenever you pop from the stack, you know the minimum element in the stack? The algorithm should have a constant complexity

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:13:22+00:00Added an answer on June 12, 2026 at 12:13 am

    Ok, here’s what you need to do..

    You need to maintain some data structure containing information of each element you insert regarding, what was the minimum and second minimum value at the time when you inserted that element..

    So, you want to have information like this: –

    For each Element pushed ->

    • minimum value after insertion
    • minimum value before insertion

    This information will be needed when you pop that element from stack.. So, you would know that, whether you are popping minimum value or not.. If yes, then you can replace the current minimum value with the minimum value before pushing this element.. If not, then there will be no change in minimum value at that time..

    For E.g: –

    Suppose currently you have following element in stack: –

    [3, 5, 7, 9, 2, 4]
    
    • And you push a value 8.. Then you have two values to maintain..
      (minimum value before 8 in pushed, i.e. 2, and miminum value after 8
      is inserted, i.e. 2): –

      min_value_before_push = min(stack)
      push 8
      min_value_after_push = min(stack)
      
    • And if you push a value 1, then minimum_before_insertion is 2,
      and minimum_after_insertion is 1: –

      min_value_before_push = 2
      min_value_after_push = 1
      

    Now your stack is :-

    [1, 8, 3, 5, 7, 9, 2, 4]
    

    Now, if you pop, You will see that for value 1: –

        min_value_before_push = 2
        min_value_after_push = 1
    

    So, popping will alter the minimum value, So, you change the current minimum value with the minimum_value_before_push of 1.. So, again your minimum is 2..

    Your current stack becomes : –

    [8, 3, 5, 7, 9, 2, 4]
    

    Now, let’s check whether this algorithm works for duplicate element : –

    Suppose you want to push a value 2 again..

    min_value_before_push = 2
    min_value_after_push = 2
    

    Then you go on and pop, you see that for value 2, min_value_after_push is 2, so this means popping it will alter the minimum value.. So you replace this value with min_value_before_push, which is also 2.. Which is what we wanted..

    NOTE: – One Benefit of this Algorithm is that, you won’t need to do much comparison.. Just a comparison with current_minimum_value while pushing.. And a comparison with current_minimum_value when you pop..

    You can try to proceed to think what data structure can you have..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a lot of different UserControls and would like to maintain consistent UI
I would like to know how I can maintain the scroll position when I
I would like to know if it is possible to maintain an authentication (like
I would like to create an auto increment function that allows me to maintain
Would be possible to extend this kind of layout so that I can have
Is it possible to have a HashTable that automatically removes the oldest accessed element
I have a large classic ASP app that I have to maintain, and I
We maintain a website built on LAMP stack that allows members to log into
I have use java stack data structure to maintain data. I have limit my
I have a .NET console app that reads info from a USB stick and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.