Output gone wrong

This is the rainwater trapping problem
I tried to write the code
but the output is coming out wrong
can you please help me out

hey,

There are a few points that you might be willing to note regarding the programming
Reason that some garbage value are being picked up is that you haven`t initialised the left and right array to be 0 so for boundary value the garbage value is being picked up .
But there is an error in the way u are computing the max left till now and right max till now, that is the reason why u arent getting the right answer

ALSO your code has a time complexity of O(n ^ 2) which would give u a TLE

I have coded the O ( n ) solution for u
Have a look

Happy coding

Thank you soo much it would be great if u could correct my code and give the solution by using arrays

1 Like

just change vector to int left[n] , right[n];
memset(left , 0 , n) to initialise all values to 0
do it for the right array as well

ok thank you so much for the help

1 Like

Yeah.
Always Happy to help

Happy Coding

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.