Trapping Rain Water Problem Statement

You are given a long type array/list ARR of size N, representing an elevation map. The value ARR[i] denotes the elevation of the ith bar. Your task is to determine the total amount of rainwater that can be trapped between these elevations.

The width of each bar is equal to 1.

Input

The first line contains an integer T which denotes the number of test cases. Each test case follows:

The first line of each test case contains an integer N, representing the size of the array/list.
The second line contains N space-separated integers representing the elevation of the bars.

Output

For each test case, print a single line containing a single integer denoting the total water that can be trapped.
The output for each test case should be printed on a new line.

Example:

Input:
T = 2 N = 4 ARR = [4, 2, 0, 3] N = 5 ARR = [5, 0, 0, 2, 1]
Output:
3 6
Explanation:

In the first test case, water can be trapped between the bars: 4 units at 1st index level 2 and 3 units at 2st index level 3.
In the second test case, the total trapped water is 6 units.

Constraints:

  • 1 <= T <= 10
  • 0 <= N <= 10^4
  • 0 <= ARR[i] <= 10^9

Time Limit: 1 second

AnswerBot
1y

The question asks to find the total amount of rainwater that can be trapped in the given elevation map.

  • Iterate through the array and find the maximum height on the left and right side of each bar.

  • Calc...read more

Help your peers!
Select
Add answer anonymously...

Unthinkable Solutions Software Developer interview questions & answers

A Software Developer was asked 9mo agoQ. Given an array of intervals where intervals[i] = [starti, endi], merge all overl...read more
A Software Developer was asked 8mo agoQ. Implement debouncing, create a nodejs server with a get / post api, a puzzle, ja...read more
A Software Developer was asked 8mo agoQ. Designing dbms for a college website

Popular interview questions of Software Developer

A Software Developer was asked 9mo agoQ1. Given an array of intervals where intervals[i] = [starti, endi], merge all overl...read more
A Software Developer was asked 7mo agoQ2. Implement debouncing, create a nodejs server with a get / post api, a puzzle, ja...read more
A Software Developer was asked 7mo agoQ3. Designing dbms for a college website
Unthinkable Solutions 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