Rain Water harvesting Problem statement

thanks @Aarnav-Jindal-1059677350830863

1 Like

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.

I am trying to make an arrays which has the greater value from the current index value from the left side

#include using namespace std; void rainWaterTrapping(int a[], int n) { int left[10]; left[-1] = 0; // Greater value findout for(int i=0; i <n; i++) { for(int j=i; j>=0; j–){ if(a[i] >= a[j-1]) { left[i] = a[j]; } } } for(int i=0; i <n; i++) { cout << left[i] << " "; } }

#include
using namespace std;
void rainWaterTrapping(int a[], int n) {
int left[10];
a[-1] = 0;
// Greater value findout
for(int i=0; i <n; i++) {
for(int j=i; j>=0; j–){
if(a[i] >= a[j-1]) {
left[i] = a[i];
}
else {
left[i] = a[j-1];
}
}

}
for(int i=0; i <n; i++) {
	cout << left[i] << " ";
}

}

int main() {
int n, arr[100];
cin >> n;

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

}

// Trying to make the left arrays which has greater value from the left sides

@Vikaspal @Vikaspal

  1. see to get answer for ith index you need to check its left maximum as well as right max.
    in your code you are not considering right maximum.
  2. no water will store at i=0 because there is no wall to left of it.
    similarly no water will store at i==n-1 becuase there is no wall right to it

I made this because the 0th index value is always maxium

If I don’t do this it will take the garbage value
void rainWaterTrapping(int a[], int n) {
int left[10];
a[-1] = 0;
// Greater value findout
for(int i=0; i <n; i++) {
for(int j=i; j>=0; j–){
if(a[i] >= a[j-1]) {
left[i] = a[i];
}
else {
left[i] = a[j-1];
}
}

}
for(int i=0; i <n; i++) {
	cout << left[i] << " ";
}

}

@Vikaspal
pls check this Rain Water harvesting Problem statement

Yes you are right we need to have the left and right max but firstly I am trying to create an arrays which will take the left max and trying to print the arrays to check that I get the correct ouptut in left array or not.
#include
using namespace std;
void rainHarvesting(int a[] , int n) {
int left[10];
for(int i=0; i<n; i++) {
for(int j=0; j<=i; j++) {
//if the values is greater or equal than current index store in the left otherwise increment till
if(a[j] >= a[i]) {
left[i] = a[j];
}
}
}
for(int i=0; i<n; i++) {
cout << left[i] << " ";
}
}
int main() {
int arr[] = {0,2,1,3,0,1,2,1,2,1};
int n = sizeof(arr)/sizeof(int);
rainHarvesting(arr,n);
return 0;
}
I am trying this but get the same array elements which are present in arr[]. If I get the left max arrays elements then it easily for the right the open.
Hoping you get my point…

after getting the boths the arrays I minus the height of the building which one is smaller elements in both the left and right tree. Is am correct ?

@Vikaspal
Hello vikas,
Initialise ur left array with 0 i.e left[i]=0;
and then in ur loop
replace statement if(a[j]>=a[i]) with statement if(a[j]>=left[i])

@Vikaspal
yeah u r correct

Can you tell me why the value is not assign by my logic

@Vikaspal
consider case when j is equal to i.
in that case
if(a[j]>=a[i] ) is equivalent to if(a[i]>=a[i]) which is always correct therfore left[i]=arr[i] and hence your all entries of left array is same as array a

No, I taking the left[i] = arr[j], I didn’t get can explain with example.
I am trying this ways, increment the j value till reaches to the i value if j value >= i value so the j value is increment each times and compare the value with a[i] , if the value is smaller than increment j , but if the j value >= then store that in the ith index value in ith index in left array,
I may be wrong but from the dry run approach it seems to correct. So that why I can’t find the loop whole in my code. Please correct me

@Vikaspal
let say we have only 2 elements in our array
index 0 1
value 3 2
when i=0

  • j=0
    a[0] >=a[0] i.e 3>=3
    left[0]=a[0] i.e left[0]=3

when i=1

  • j=0
    a[0] >=a[1] i.e 3>=2
    left[1]=a[0] i.e left[1]=3
  • j=1
    a[1] >=a[1] i.e 2>=2
    left[1]=a[1] i.e left[1]=2

so final values of left array will be
index 0 1
value 3 2

if u take any big example u will find that left array is same as array a

Okay great this is bcoz the = condition mention right it will update the arrays even the arrays already hold the max value in previous steps right ?
Right max :
int right[] = {0};
for(int i=n-1; i>=0; i–) {
for(int j=i; j<n; j++) {
if(a[j] >= right[i]) {
right[i] = a[j];
}
}
}
But get the single same value

@Vikaspal
replace int right[]={0} with int right[n]={0};

thanks @aman212yadav for resolving the issues. one thing more when we don’t mention the size of array it will taken automatically, what will happen in our case?