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

Problem of the day

You have been given a long type array/list * 'arr’* of size

It represents an elevation map wherein * 'arr[i]’* denotes the elevation of the 'ith' bar.

Print the total amount of rainwater that can be trapped in these elevations.

```
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.
```

Detailed explanation

```
4
2 1 1 4
```

```
2
```

```
Water trapped by:
block of height 2 is 0 units.
block of height 1 is 1 unit.
block of height 1 is 3 1 unit.
block of height 4 is 3 0 units.
Hence the total is 2.
```

```
5
8 1 8 2 4
```

```
9
```

```
Water trapped by:
block of height 8 is 0 units.
block of height 1 is 7 units.
block of height 8 is 0 units.
block of height 2 is 2 units.
block of height 4 is 0 units.
Hence the total is 9.
```

```
The expected time complexity is O(n).
```

```
0 <= n <= 10^6
0 <= arr[i] <= 10^9
Time Limit: 1 sec
```