Trapping Rain Water II Problem Statement
Given an M * N matrix where each cell's value represents its height in a 2-D elevation map, calculate the total volume of water that can be trapped after rainfall.
Input:
The first line contains an integer ‘T’ representing the number of test cases.
For each test case:
- The first line contains two integers 'M' and 'N', indicating the number of rows and columns respectively.
- The following 'M' lines, each containing 'N' space-separated integers, denote the height of each cell in the matrix.
Output:
For each test case, output a single integer representing the volume of water that can be trapped.
Print each result on a new line.
Example:
Input:
1
3 3
5 5 5
5 2 3
6 9 8
Output:
1
Explanation:
The given matrix forms a 2-D elevation map where, after rain, water is trapped leading to a total volume of 1 unit.
Constraints:
1 <= T <= 10
1 <= M,N <= 100
1 <= matrix[i][j] <= 10^3
Note: You aren’t required to handle the printing of results; this is managed elsewhere. Your task is to implement the function to determine the trapped volume.

AnswerBot
4mo
Calculate the total volume of water that can be trapped in a 2-D elevation map after rainfall.
Iterate through each cell in the matrix and calculate the trapped water based on the surrounding heights.
U...read more
Help your peers!
Add answer anonymously...
MindTickle Software Developer interview questions & answers
A Software Developer was asked 6mo agoQ. Design the Low-Level Design (LLD) for a Parking Lot.
A Software Developer was asked Q. Ninja and His Meetings Problem Statement Ninja has started a new startup with a ...read more
A Software Developer was asked Q. Smallest Subarray With K Distinct Elements Given an array A consisting of N inte...read more
Popular interview questions of Software Developer
A Software Developer was asked Q1. Ninja and His Meetings Problem Statement Ninja has started a new startup with a ...read more
A Software Developer was asked Q2. Smallest Subarray With K Distinct Elements Given an array A consisting of N inte...read more
A Software Developer was asked Q3. Trapping Rain Water II Problem Statement Given an M * N matrix where each cell's...read more
Stay ahead in your career. Get AmbitionBox app


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
AmbitionBox Awards
Get AmbitionBox app

