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 819323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:17:22+00:00 2026-05-15T02:17:22+00:00

Please note that there is no limitation of memory. I need to insert int

  • 0

Please note that there is no limitation of memory.
I need to insert int from 1 to 1000.

I can do the each of the following operations in constant order of time:

  1. push():adds to the top
  2. pop():removes the top element
  3. getMax(): returns the max element

Please suggest me appropriate datastructure.

  • 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-05-15T02:17:23+00:00Added an answer on May 15, 2026 at 2:17 am

    Since there is no limitation of memory, I will use 2 vectors – one for the actual data on the stack, and the other to keep track of the max at every state of the stack.
    For simplicity sake I’m assuming this stack holds only +ve ints.
    I know this doesn’t have any error checking. But I am just providing the data structure idea here, not the full-fledged solution.

    class StackWithMax
    {
     public:
      StackWithMax() : top(-1), currentMax(0) { }
      void push(int x);
      int pop();
      int getMax() { return m[top]; }
     private:
      vector<int> v; // to store the actual elements
      vector<int> m; // to store the max element at each state
      int top;
      int currentMax;
    };
    
    void StackWithMax::push(int x)
    {
      v[++top] = x;
      m[top] = max(x, currentMax);
    }
    
    int StackWithMax::pop()
    {
      int x = v[top--];
      currentMax = m[top];
      return x;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please note: In each step I describe below I'm logged in as the same
Please note the Edit below for a lot more information, and a possible solution
Please note - I am not looking for the right way to open/read a
How do I print debug messages in the Google Chrome JavaScript Console? Please note
I have searched around, and it seems that this is a limitation in MS
I've read before here on SO ( EDIT: Incremental Checksums ) that there are
Please suggest some good resources to start writing Java Web services.
Please read the whole question. I'm not looking for an approach to managing multi-lingual
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
Please give me the direction of the best guidance on the Entity Framework.

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.