It is giving tle .Plzz help me to find a better approach
Rain water harvesting problem
Hey @Alfred
That’s because time complexity of your code is O(N*N) and we have to do this question in O(N) time complexity.
This may help
An element of an array can store water if there are higher bars on left and right. We can find the amount of water to be stored in every element by finding the heights of bars on the left and right sides. The idea is to compute the amount of water that can be stored in every element of the 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.
Pre-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.
Hey please help why my code gives segmentation fault
Make arrays global or declare them dynamically
Why we declare them global
Inside functions u can create arrays close to 10^6 , u have created 3 i.e why segmentation
And globally u can create close to 10^8
This is because of limited stack size
Thanku for your help
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.