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.

AnswerBot
4mo

Given an array representing an elevation map, find the maximum amount of rainwater that can be trapped between the bars.

  • Iterate through the array and calculate the maximum height on the left and right...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Interview Questions Asked at Mobikwik

Q. Maximum Subarray Sum Problem Statement Given an array of integers, determine the...read more
Q. Reverse Linked List Problem Statement Given a singly linked list of integers, re...read more
Q. Kth Smallest Element Problem Statement You are provided with an array of integer...read more
Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits