Stock Span problem


why testcase 2 and 3 showing me error?

You need to modify the approach you are using in your code as :

stack< long long int >s;
long long int ans[1000001];
s.push(0);
ans[0]=1;
for(int i=1;i<N;i++)
{
while(!s.empty() && ar[i]>=ar[s.top()])
{
s.pop();
}
if(!s.empty())
{
ans[i]=(i-s.top());
}
else
{
ans[i]=i+1;
}
s.push(i);
}