Trapping Rain Water

You have been given a long type array/list 'ARR' of size 'N'. It represents an elevation map wherein 'ARR[i]' denotes the elevation of the 'ith' bar. Print the total amount of rainwater that can be trapped in these elevations.

Note :
The width of each bar is the same and is equal to 1.
Input format :
The first line contains an integer 'T' which denotes the number of test cases. Then the test cases follow.

The first line of each test case contains an integer 'N' representing the size of the array/list.

The second line contains 'N' single space-separated integers representing the elevation of the bars.
Output Format :
For each test case, print in a single line a single integer denoting the total water that can be trapped.

The output of each test case will be printed in a separate line.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
0 <= N <= 10^4
0 <= ARR[i] <= 10^9

Time Limit : 1 sec
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

CodingNinjas
author
2y

The idea is to traverse every array element and find the highest bars on the left and right sides. Take the smaller of two heights. The difference between the smaller height and height of the current ...read more

CodingNinjas
author
2y
Brute Force Approach

The idea here is to travel over every elevation on the map and calculate the units of water the elevation can store.

Here is the algorithm :

  1. Iterate over every elevation or element...read more
CodingNinjas
author
2y
Left And Right Array Approach

The idea here is to pre-compute the maximum elevation on the left and right for every element in a most optimized way.

Here is the algorithm :

  1. Create two lists or arrays, ...read more
CodingNinjas
author
2y
Peak Elevation Approach

The idea here is to find the maximum elevation in the array by iterating over the array once. Say, the max elevation or height is ‘PEAK’.

Taking the ‘PEAK’ as a reference point,...read more

Add answer anonymously...
Unthinkable Solutions 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
Get AmbitionBox app

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