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
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...
Mphasis Senior Software Engineer 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