Trapping Rain Water

You are provided with an array ARR of integers representing an elevation map, where ARR[i] denotes the elevation of the ith bar. Determine the maximum amount of rainwater that can be trapped between these bars.

Input:

The first line contains an integer 'T', the number of test cases.
For each test case:
The first line contains an integer 'N', the size of the array/list.
The second line consists of 'N' space-separated integers representing the heights of the bars.

Output:

For each test case, output a single integer representing the total volume of trapped rainwater.

Example:

Input:
2
4
0 1 0 2
6
0 1 3 0 1 2

Output:
1
5

Constraints:

  • 1 <= T <= 10
  • 0 <= N <= 104
  • 0 <= ARR[i] <= 109
  • Time Limit: 1 sec

Note:

The width of each bar is 1. You are not required to print anything; the printing is already taken care of by the provided function definition.

Be the first one to answer
Add answer anonymously...
Mobikwik Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter