Array ( [0] => [1] => questions [2] => Arrays [3] => Trapping-Rain------------- ) Arrays | Trapping Rain | THE INQUISITIVE





Trapping Rain

LEVEL:Beginner

Description

Given an array of integers. You have to find the how much quantity of water can store in between the elements.

Input Format

First line contains the size of the first array. Next line contains the m integers separated by the spaces.

Output Format

Print the amount of quantity can store.


Example 1:

Input
3
2 0 2
Output
2
Example 2:

Input
5
3 0 2 0 2
Output
7
Example 3:

Input
12
0 1 0 2 1 0 1 3 2 1 2 1
Output
6

oops

Login to see Discussion




Approach

Approach1:

Step-i) Iterate through the array and find non-zero elements.
Step-ii) Take no. of zeroes between 2 non-zero elements as length of the tank.
Step-iii) Height can be taken as the minimum value of 2 adjacent non-zero elements.
Step-iv) Multiply height and length for every 2 adjacent elements and sum up them.
Step-v) The final sum is the required answer.

Time Complexity: O(n)
Space Complexity: O(n)


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution