this code is same as one on gfg.i cant understand the error
code:
#include
#include
using namespace std;
int maxarea(int hist[], int n){
stack s;
int tp; //to store the top
int areaWithtop;
int maxarea=0;
int i=0;
while(i<n){
//push element if top is smaller than current
if(s.empty() or hist[s.top()] <= hist[i]){
s.push(i++);
}
//
else{
tp=s.top();
s.pop();
areaWithtop=hist[i]*(s.empty()?i:i-s.top()-1);
//update for maxarea
if(maxarea<areaWithtop){
maxarea=areaWithtop;
}
}
}
//if still bars are left,pop them all out
while(!s.empty()){
tp=s.top();
s.pop();
areaWithtop=hist[i]*(s.empty()?i:i-s.top()-1);
//update for maxarea
if(maxarea<areaWithtop){
maxarea=areaWithtop;
}
}
return maxarea;
}
int main(){
int n; //no of bars
cin>>n;
int hist[100000];
for(int i=0;i<n;i++){
cin>>hist[i];
}
cout<<maxarea(hist,n);
}