Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. 

For water to get collected on Bar B, There should be Bar on Left side and Right Side of Bar B whose height is greater than Bar B height otherwise water will overflow.

So for each bar, We can find amount of water it will store by checking the max heights of bars on left and right sides.

 

Once the bar with max height on left side and max height on right side is evaluated.
Check the minimum among both as that will be the level in which water will be accumulated and rest water will overflow.
So the amount of water a bar hold = Minimum among( max on left side, max on right side) – current bar height