Rain water harvesting brute force

not getting output on checking

you are doing mistake in making loops
check it

	for(int k=i-1;i>=0;i++){

this will lead to infinite loop

if you increase i then will always >=0

NOW also not getting output

I have checked now also not working

for(int j=i+1;i<n;i++){

for(int k=i-1;i>=0;i--){

in both the loops variable is j and k so why you are increasing and decreasing i resp.

also in each iteration you have to assume maxl and maxr= arr[i]

Modified Code

i hope this helps
if yes hit a like and don’t forgot to mark doubt as resolved :grinning:
if you have more doubts regarding this feel free to ask

Interview_Preparation_C_Rain_Water_Harvesting_i_class_fab_fa_facebook_i_i_class_fab_fa_apple_i_i_class_fab_fa_amazon_i_N_i_class_fab_fa_google_i_

all test cases are not correct

this is because your approach is not optimised
i will give TLE

try to optimised it

Hint:
instead of calculating left max every time
you can make a single variable LeftMax

and update it every time
if(LeftMax<a[i]) LeftMax=a[i];

how but to find left max we will have to calc that na?

is making maxl and maxr array for this is ok?

this approach is okay
but time complexity of this approach is large
to optimized this code
don’t calculate left max every time

see the code again


i have done optimization in it

now also 3 cases are coming wrong

if it is still give tle
then you have to do some precomputation
you have to make right max array outside the loop

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.