Rain water Harvestin gProblem

Can you please tell me whats the problem in my code for this question

#include<bits/stdc++.h>

using namespace std;

int main(){

int n;
cin>>n;
int *arr=new int[n];

for(int i=0;i<n;i++){
    cin>>arr[i];
}

vector<int> vec;
if(arr[0]>arr[1])
    vec.push_back(0);
for(int i=1;i<n-1;i++){
    if(arr[i]>=arr[i-1]&&arr[i]>=arr[i+1])
        vec.push_back(i);
}

if(arr[n-1]>arr[n-2])
    vec.push_back(n-1);
int high,save=0;
for(int i=0;i<vec.size()-1;i++){
    high=min(arr[vec[i]],arr[vec[i+1]]);
    for(int j=vec[i]+1;j<vec[i+1];j++){
        save+=high-arr[j];
    }
}
cout<<save<<endl;

return 0;

}

Hi

Problem in your logic

See this video tutorial https://www.youtube.com/watch?v=Uog2Jmyb3iY

Hit like if you get it!
Cheers!

i have already seen that video…i want to know the problem in my logic…

Do a dry over this test case

3
2 0 2

Hit like if you get it!
Cheers!

its giving 2 bro…
and i think that is correct

Prajawal

  1. u have to first find min(largest left ,largest right) of a block but when u dealing with a corner case then it shou;d not store any water so the vector[0] and vector[n-1] should only contain the height of the array.

  2. know as u calculate the water store ur save variable should only contain value when hight-arr[j]>0
    for ur code if value is negative then it should contribute to it
    hope it help
    for code reply

1 Like