AlgoUniversity
  • Go Back
Discussion
Trapping Rainwater :

Author

Ayush Kumar Shaw

Difficulty Level : Medium

Submissions : 274

Asked In : Amazon

Marks :10

: 5 | : 0

You ar given an array $$$a$$$ consisting of $$$n$$$ integers representing the height map where the width of each bar is $$$1$$$ unit. If $$$a_i=k$$$, this implies that the height of the $$$i^{th}$$$ bar extending from $$$x=i$$$ to $$$x=i+1$$$ is $$$k$$$. Rainwater gets trapped between these bars when it rains. Find how many units of water will get trapped when it rains.

Input

The first line contains a single integer $$$n(1 \leq n \leq 2 \times 10^5)$$$. The next line contains $$$n$$$ spaces integers $$$a_1,a_2,...,a_n(0 \leq a_i \leq 10^6)$$$.

Output

Output a single integer - the amount of water that can be trapped when it rains.

Example

Input
6
5 0 3 5 9 1
Output
7

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.