#include
#include
using namespace std;
int main()
{
int n;
cin>>n;
int *arr=new int[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
stack s;
int area=0,maxarea=0,top=0;
int i=0;
while(i<n)
{
if(arr[s.top()]<=arr[i] || s.empty())
{
s.push(i++);
}
else
{
top=s.top();
s.pop();
}
if(s.empty())
{
area=arr[top]i;
}
else
{
area=arr[top](i-s.top()-1);
}
if(area>maxarea)
{
maxarea=area;
}
}
while(!s.empty())
{
top=s.top();
s.pop();
if(s.empty())
{
area=arr[top]i;
}
else
{
area=arr[top](i-s.top()-1);
}
if(area>maxarea)
{
maxarea=area;
}
}
cout<<maxarea;
}
Please check this code its not running
@supratik260699
how can I check your code. Ok do one thing, directly submit your code. it’ ll check it from here
I have Submitted the code
@supratik260699
you code is giving segmentation fault that means it is trying to access stack.top() even when stack is empty.
Change line 18 to if( s.empty() || arr[s.top()]<=arr[i] ). Actually previously in your code, during execution your code first check s.top() which doesn’t exist if s.empty() is true.
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.

