This is a question from Hackerearth:-
Please can someone help me how my solution is wrong/ which test cases am I missing ?
My Solution:-
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ll n;
cin>>n;
ll c=0;
stack st;
for(ll i=0;i<n;i++)
{
ll a;
cin>>a;
if(st.empty())
{
st.push(a);
}
else
{
if(st.top() > a)
{
st.push(a);
}
else
{
while(!st.empty() and st.top() < a)
{
st.pop();
c++;
}
st.push(a);
}
}
}
cout<<c<<endl;
}
I have done it using simple stack and test cases fail and I don’t understand why do I need to store the postions of next and prev as mentioned by the editorial. Please help!