Cannot identify what is wrong
Rain water harvesting question
Hello @Aditiverma,
Do the following modification in your code:
if(left>right){ ans = ans+left-arr[i]; cout<<"1:"<<ans<<" "; } else{ ans = ans +right-arr[i]; cout<<"2:"<<ans<<" "; }
This would help you to understand the flow of your program.
Is it executing the way you want it to?
If not, try to correct your approach.
If you are stuck, think of a different approach.
If you still face issue, feel free to ask. But, try hard first.
Hope, this would help.
Give a like, if you are satisfied.
i wanted to ask when we are iterating the array https://ide.codingblocks.com/s/106513 from left to get max and then from right to get max so when we apply the formula i am considering arr[i] and arr[j] which are opposite in direction so how can i iterate from start for ans
-
How are you so sure that there would not be more than 1000 elements in the array.
-
Your code was taking O(n^2) time complexity.
You are required to do it in O(n). -
The logic you are applying is incorrect.
I have modified it, where i am finding the highest bars on left and right sides. Take the smaller of two heights. The difference between the smaller height and height of the current element is the amount of water that can be stored in this array element.
in the following code, i am pre-computing highest bar on left and right of every bar in O(n) time. Then use these pre-computed values to find the amount of water in every array element.
Hope, this would help.
Give a like, if you are satisfied.