whats wrong in this code
Rainwater harvesting doubt
your code is not producing correct output for the following input
12
0 1 0 2 1 0 1 3 2 1 2 1
expected output: 6
your output: 11
i know mam
it is not giving correct output
thats why m asking what i have missed in this code
or where m i wrong??
your logic isn’t working for all cases
try this
An element of array can store water if there are higher bars on left and right. We can find amount of water to be stored in every element by finding the heights of bars on left and right sides. The idea is to compute amount of water that can be stored in every element of array. For example, consider the array {3, 0, 0, 2, 0, 4}, we can store two units of water at indexes 1 and 2, and one unit of water at index 2.
A Simple Solution is to traverse every array element and find the highest bars on left and right sides. Take the smaller of two heights. The difference between smaller height and height of current element is the amount of water that can be stored in this array element. Time complexity of this solution is O(n2).
An Efficient Solution is to prre-compute 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.
if you still want some help, i will help you with code