#include
#include
using namespace std;
void span(int a[], int n, int c[])
{
stacks;
s.push(0);
c[0]=1;
for(int i=1;i<n;i++)
{
while(a[i]>a[s.top()] and !s.empty())
s.pop();
if(s.empty())
c[i]=i+1;
else
c[i]=i-s.top();
s.push(i);
}
}
int main() {
int n;
cin>>n;
int a[n],c[n]={0};
for(int i=0;i<n;i++)
cin>>a[i];
span(a,n,c);
for(int i=0;i<n;i++)
cout<<c[i]<<" ";
cout<<“END”;
return 0;
}
Please check my code
your while loop condition is not correct
correct one
while(!s.empty() and a[i]>a[s.top()])
first you have to check whether stack is empty or not then compare element using s.top();
because if s is empty s.top() will give error
i hope this helps
if yes hit a like and don’t forgot to mark doubt as resolved 
if you have more doubts regarding this feel free to ask
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.