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:
- Push the element x into the stack. 
- Delete the element present at the top of the stack. 
- 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;
}
 
     
    