-1

I am working on a programming exercise Maximum Element (https://www.hackerrank.com/challenges/maximum-element/problem?isFullScreen=false) using C++, the goal of the exercise is to print maximum element for each type 3 query. Each query is one of these three types:

  1. Push the element x into the stack.

  2. Delete the element present at the top of the stack.

  3. Print the maximum element in the stack.

For inputs:

10
1 97
2
1 20
2
1 26
1 20
2
3
1 91
3

Expected Output is:

26
91

My code is printing:

0
0

My code (written below) is clearly printing the wrong answer however I can't find out where have I done the mistake. How can I reason through this problem, or debug my error?

#include<iostream>
#include<stack>
using namespace std;
int main() {
    int n;
    cin>>n;

    while(n--)
    {
        stack<int> s;
        int a;

        cin>>a;

        if(a==1)
        {
            int x;
            cin>>x;
            s.push(x);

        }
        else if (a==2)
        s.pop();

        else {
        int max =0;
        while(!s.empty())
        {
            if(s.top()>max)
            max=s.top();

            s.pop();

        }
        cout<<max<<endl;
        }
    }   
    return 0;
}
shivanik6z
  • 19
  • 4
  • 1
    Why are you popping from the stack when you are supposed to be finding the maximum element? You should probably just keep track of the maximum element currently in the stack. – cigien May 07 '20 at 18:30
  • 1
    `#include using namespace std;` - *No*. Just no. Don't *ever* do that. Please read all of [Why should I not #include ?](https://stackoverflow.com/q/31816095/5910058) right now. – Jesper Juhl May 07 '20 at 18:31
  • 1
    I'm starting to wonder if it makes sense to have a filter for posts that contain `#include` – cigien May 07 '20 at 18:32
  • Hint: [std::max_element](https://en.cppreference.com/w/cpp/algorithm/max_element) already exist. You don't need to roll your own inferior version of it. Just use what's already there. – Jesper Juhl May 07 '20 at 18:34
  • 1
    @cigien yeah. And have the filter just delete them... – Jesper Juhl May 07 '20 at 18:35

3 Answers3

1

You declared the stack stack<int> s; inside the loop, so it will be cleared at every beginning of the loop. The declaration should be outside the loop like

stack<int> s; // move here, for example
int n;
cin>>n;
while(n--)
{
    // stack<int> s;

This change will make the output for the input here correct, but I don't think the program is correct with only this change. I don't think that the type 3 query should remove elements in the stack.

MikeCAT
  • 61,086
  • 10
  • 41
  • 58
0

1) declared the stack out of the loop 2) previously the elements were being popped out for finding the maximum element so made another stack to keep track of the maximum element

New code passed all the test cases:-

#include <iostream>
#include <stack>
using namespace std;
int main() {
  stack<int> s, max;
  max.push(0);
  int n;
  cin >> n;
  while (n--) {
    int a;
    cin >> a;
    if (a == 1) {
      int x;
      cin >> x;
      s.push(x);
      if (x >= max.top())
        max.push(x);
    } else if (a == 2) {
      int q = s.top();
      if (q == max.top())
        max.pop();
      s.pop();
    } else {
      cout << max.top() << endl;
    }
  }
  return 0;
}
shivanik6z
  • 19
  • 4
-1

You declared stack inside the loop which with every increment or decrement in this case, will set the value back to initial state.

A quick fix would be to just declare the stack outside of your loop and the rest code looks fine.

Asfand
  • 1
  • 1