Test Case 4 Not passed

CODE;-

#include
using namespace std;

int main() {

int n;
cin>>n;

int a[n];

for(int i=0;i<n;i++){

    cin>>a[i];

}

int i=0;
while(a[i]<=0){
i++;
}

int A,B,C;
int end=0;

int area=0,sub=0,width=0,h=0,water=0;
while(i<n-2){
A=i;
B=A+1;
C=B+1;
end=0;
width=0;
sub=0;
while(end==0){
width++;
A++;
B++;
C++;
sub=sub+a[A];

if(a[A]<a[B] && a[B]>a[C]){


    end=B;
    if(a[i]>=a[B]){
        h=a[B];
    }else{
        h=a[i];
    }

    area= h*width-sub;
    water=water+area;
    sub=0;
    area=0;
    h=0;
    width=0;
    i=B;

}

}
}

cout<<water;

return 0;

}

Hello @myself.yash.srivastav,

From next time, send your code using Online Coding Blocks IDE.
The way you have shared it, is introducing many run-time errors to it.
Steps:

  1. Paste your code on IDE.
  2. Save your code there.
  3. Share the URL generated.

Now, coming back to your doubt:
It is giving wrong output for test cases like:
8
0 1 3 4 5 2 3 0
Expected Output:
1
Your Output:
-4

Suggestion:
Dry run your code on the above input.

Hope, this would help.
If you still can’t find it, let me know.

Hello @myself.yash.srivastav,

Your modified code is failing for the testcases like:
example 1:
6
3 0 0 2 0 4
Expected Output:
10
Your Output:
4

Reason:
if(a[A]<a[B] && a[B]>a[C]){}
The above mentioned condition doesn’t account for the above specified testcase.
This is because you are checking the value at index B with C, it is possible that a building with greater height is present after position C.

Hope, this would help.
Give a like, if you are satisfied.

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.