#include<bits/stdc++.h>
using namespace std;
long long res(int * a,int n)
{
stack s;
int t;
long long curr,area=0;
int i=0;
while(i<n)
{
if(s.empty()||a[s.top()]<=a[i])
{
s.push(i);
i++;
}
else
{
t=s.top();
s.pop();
if(!s.empty())
curr=a[t]*(-s.top()+i-1);
else
curr=a[t]i;
if(curr>area)
area=curr;
}
}
while(!s.empty())
{
t=s.top();
s.pop();
if(!s.empty())
curr=a[t](-s.top()+i-1);
else
curr=a[t]*i;
if(curr>area)
area=curr;
}
return area;
}
int main() {
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
cout<<res(a,n);
return 0;
}
Please help me to find error in code
@dakshi Your code seems to be correct. Please once try it submitting after changing int to long long int.
You will also need to convert your array to long long because for calculating the answer their multiplication is taking place.
I have done that change in your code, you can see at https://ide.codingblocks.com/s/224522
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.