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 that this is a different question from the already answered How to
Please note that this question is from 2008 and now is of only historic
Can I write XSD schema for this? Please note that the category is repeated
How can I convert Persian date to Gregorian date using System.globalization.PersianCalendar? Please note that
Please note that I'm not asking how but why. And I don't know if
(Please note that I have seen a similar question on StackOverflow recently, however I
Please note that this is not homework and i did search before starting this
Please note that I did ask this on Super User and received absolutely no
When open a query in Microsoft project, the start date changes. Please note that,
I mean things like: FK1 -> 1FK2 -> 2PK Please, note that 1FK2 is

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.