Rain Water Trapping Problem Statement

Given an array/list ARR of size N, representing an elevation map where each element ARR[i] denotes the elevation of the i-th bar. Your task is to calculate and print the total amount of rainwater that can be trapped between these elevations.

Example:

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

The elevation map is represented by array ARR, and 6 units of rainwater can be trapped between these elevations.

Constraints:

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

The width of each bar is the same and is equal to 1. You are not required to print anything; it has already been taken care of. Just implement the function to calculate the trapped water.

AnswerBot
4mo

Calculate the total amount of rainwater that can be trapped between given elevations in an array.

  • Use two-pointer approach to keep track of left and right boundaries.

  • Calculate the trapped water by find...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Interview Questions Asked at Samsung

Q. All 1's and 2's of array
Q. Design a ticket booking system like Bookmyshow.
Q. How do you find the second largest salary in a DBMS?
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