I am getting “TLE” after getting correct o/p till 15 test cases, what is the mistake ?
problem link – Maximum Element | HackerRank
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
int main()
{
stack<int> s;
stack<int> dup;
int n;
cin >> n;
while(n--)
{
int t,x;
cin >> t;
if(t==1)
{
cin >> x;
s.push(x);
}
else if(t==2)
{
s.pop();
}
else
{
int maxi = 0;
while(!s.empty())
{
dup.push(s.top());
maxi = max(maxi,s.top());
s.pop();
}
while(!dup.empty())
{
s.push(dup.top());
dup.pop();
}
cout << maxi << '\n';
}
}
return 0;
}