Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Last Updated: 31 Jul, 2020

Moderate

```
The width of each bar is the same and is equal to 1.
```

```
Input: ‘n’ = 6, ‘arr’ = [3, 0, 0, 2, 0, 4].
Output: 10
Explanation: Refer to the image for better comprehension:
```

```
You don't need to print anything. It has already been taken care of. Just implement the given function.
```

```
The first line of each test case contains an integer 'n' representing the size of the array/list.
The second line contains 'n' single space-separated integers representing the elevation of the bars.
```

```
Return an integer denoting the amount of water that can be trapped.
```

The idea here is to travel over every elevation on the map and calculate the units of water the elevation can store.

Here is the algorithm :

- Iterate over every elevation or element and find the maximum elevation on to the left and right of it. Say, the maximum elevation on to the left of the current elevation or element that we are looking at is ‘maxLeftHeight’ and the maximum elevation on to the right of it is ‘maxRightHeight’.
- Take the minimum of ‘maxLeftHeight’ and ‘maxRightHeight’ and subtract it from the current elevation or element. This will be the units of water that can be stored at this elevation.
- Compute units of water that every elevation can store and sum them up to return the total units of water that can be stored.

The idea here is to pre-compute the maximum elevation on the left and right for every element in a most optimized way.

Here is the algorithm :

- Create two lists or arrays, say, ‘leftMax’ and ‘rightMax’.
- At every index in the ‘leftMax’ array store the information of the ‘leftMaxHeight’ for every elevation in the map.
- Similarly, at every index in the ‘rightMax’ array store the information of the ‘rightMaxHeight' for every elevation in the map.
- Iterate over the elevation map and find the units of water that can be stored at this location by getting the left max height and right max height from the initial arrays that we created.

The idea here is to find the maximum elevation in the array by iterating over the array once. Say, the max elevation or height is ‘peak’.

Taking the ‘peak’ as a reference point, we can say that we may store the water onto the left and right of this point.

Here is the algorithm:

- Get the ‘peak’ value
- Move from left to right in the elevation map towards the ‘peak’. Maintain three variables:
- ‘maxSoFar’ is the maximum elevation that has been encountered on the way towards ‘peak’.
- ‘countSubmerged’ which are the elevations that are smaller than the ‘maxSoFar’.
- ‘submergedArea’ which is ‘arr[i]' * 1.

- Now, every time a new ‘maxSoFar’ is reached, add ('maxSoFar' * ‘countSubmerged’ - ‘submergedArea’) to a running sum.
- Repeat the same from right to left.

Similar problems

Longest Subarray With Zero Sum

Moderate

Posted: 3 Nov, 2022

Merge Two Sorted Arrays Without Extra Space

Moderate

Posted: 19 Nov, 2022

Merge Two Sorted Arrays Without Extra Space

Moderate

Posted: 19 Nov, 2022

Ninja And The Strictly Increasing Array

Moderate

Posted: 27 Nov, 2022

Negative To The End

Easy

Posted: 16 Dec, 2022

Find Duplicate in Array

Easy

Posted: 5 Jun, 2023