CPP : Stock span Problem

I know why my test case 2 is failing but unable to decide how to modify the code.
My code is :

Pls add comments in your code so that I could understand that…


This is the updated code, pls look into it.

The approach you are using in your code is not correct, Try using this approach,
stacks;
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);
}