PREP19 Editorial

Problem Explanation

You are given an array of heights of N bars. You have to tell the maximum amount of water that can be stored in these bars after raining.

Approach

We use the two pointer approach. We can take the pointers - left and right, and iterate over the array till left < right. At each turn we save the maximum left bar and right bar as the water will be stored in between and maximum left and right bars and the level of water will be minimum of these two. At each turn we can add the amount of water stored at that position, which is the water level minus the height of that bar.