#include
#include
using namespace std;
int main() {
int a[100],i,n,x,result=0;;
cin>>n;
for(i=0;i<n;i++){
cin>>a[i];
}
for(i=0;i<n-1;i++){
if((a[i]<a[i-1])&&(a[i]<a[i+1])){
result=result+(min(a[i-1],a[i+1])-a[i]);
}
}
cout<<result;
return 0;
}
Wrong test case
hey,you are just checking the heights of adjacent bar for every bar but it will give you wrong answer,you should calculate leftmost highest and rightmost highest bar for every bar then for every bar your result will be min of those two bars you calculated.
you should calculate result as result=result+min(left_highest of that index , right_highest of that index)-a[index] and remember your min of this two highest should be greater than a[index] too.
hey @vishwash.bajpai87 did you get your answers or if still you are facing any problem you can ask me else please mark this doubt as resolved.Thankyou.
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.