Trapping Rain Water
Submissions: 23898   Accuracy:

42.45%

  Difficulty: Medium   Marks: 4

Given an array A of N non-negative integers representing height of blocks at index i as Ai where the width of each block is 1. Compute how much water can be trapped in between blocks after raining.
Structure is like below:
|  |
|_|
We can trap 2 units of water in the middle gap.

Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows. Each test case contains an integer N followed by N numbers to be stored in array.

Output:
Output the total unit of water trapped in between the blocks.

Constraints:
1 <= T <= 100
3 <= N <= 107
0 <= Ai <= 107

Example:
Input:

2
4
7 4 0 9
3
6 9 9

Output:
10
0

Explanation:
Testcase 1:
Water trapped by block of height 4 is 3 units, block of height 0 is 7 units. So, total unit of water trapped is 10 units.

 

** For More Input/Output Examples Use 'Expected Output' option **

Author: Karan Grover


Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.