Not getting correct output
mistakes
-
your are accessing s.top() without checking s is empty or not
-
in while loop condition
while(arr[i]<arr[top] && !s.empty()){
first you have to check whether s is empty or not then check the condition -
outside this loop
long long int size=s.size();
if(!s.empty())
{
long long int top=s.top();
s.pop();
area=arr[top]*(size-s.size());
if(area>max_area)
{
max_area=area;
}
}
this is completely wrong
you have to do same thing as done above
like this
while (!s.empty())
{
long long int top = s.top();
s.pop();
if (s.empty())
{
area = arr[top] * i;
if (area > max_area)
{
max_area = area;
}
}
else
{
area = arr[top] * (i - 1 - s.top());
if (area > max_area)
{
max_area = area;
}
}
}
you can see all the changes in modified Code below
Modified code
if you have more doubts regarding this feel free to ask
i hope this helps
if yes hit a like
: and don’t forgot to mark doubt as resolved 