Please tell me what is the issue in this code:
My code is running smoothly on codeblocks but on coding blocks’s IDE it is showing following error:
Error !
/bin/run.sh: line 4: 18 Segmentation fault (core dumped) ./exe
Please tell me what is the issue in this code:
My code is running smoothly on codeblocks but on coding blocks’s IDE it is showing following error:
Error !
/bin/run.sh: line 4: 18 Segmentation fault (core dumped) ./exe
Issue is on line 21 :
for( int i=0;i<=b.size()-2;i++){ //here -2 instead of -1
Anyway ur logic is incorrect
Some examples where it will fail
[10 1 10]
[10 2 5 2 5 10 2]
Correct logic O(n^2)
An element of the 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 left and right sides. The idea is to compute the 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 three units of water at indexes 1 and 2, and one unit of water at index 3, and three units of water at index 4.
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 the smaller height and height of the current element is the amount of water that can be stored in this array element. Time complexity of this solution is O(n2).
Better Approach O(n)
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.
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.