`{"id":"a5194502-ab57-4757-96e2-44d3690503e8","name":"Trapping Rain Water","description":"1. Given n non-negative integers representing an elevation map where the width of each bar is 1.\r\n2. Compute how much water it can trap after raining.\r\n","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. n == height.length\r\n2. 0 &lt;= n &lt;= 3 * 10^4\r\n3. 0 &lt;= height[i] &lt;= 10^5","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\n\nint trap(vector<int>& height) {\n \n\n}\n\n\nint main()\n{\n int n;\n cin >> n;\n vector<int>arr(n, 0);\n for (int i = 0; i < n; i++)\n {\n cin >> arr[i];\n }\n cout << trap(arr);\n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static int trap(int[] height) {\r\n return 0;\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int n = Integer.parseInt(read.readLine());\r\n int arr[] = new int[n];\r\n for(int i=0;i<n;i++)arr[i] = Integer.parseInt(read.readLine());\r\n int result = trap(arr);\r\n System.out.println(result);\r\n \r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"12\r\n0\r\n1\r\n0\r\n2\r\n1\r\n0\r\n1\r\n3\r\n2\r\n1\r\n2\r\n1","sampleOutput":"6\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"e679571d-6e14-447b-824e-8f0b0fd80b7c","name":"Trapping Rain Water","slug":"trapping-rain-water","type":1}],"next":{"id":"747b9f03-71da-434d-963b-9914110996ed","name":"Trapping Rain Water Hard MCQ","type":0,"slug":"trapping-rain-water-hard-mcq"},"prev":{"id":"c566a0f1-9323-41c1-8954-951c23a71519","name":"Remove Duplicate Letters","type":3,"slug":"remove-duplicate-letters-9481"}}}`

# Trapping Rain Water

1. Given n non-negative integers representing an elevation map where the width of each bar is 1. 2. Compute how much water it can trap after raining.

`{"id":"a5194502-ab57-4757-96e2-44d3690503e8","name":"Trapping Rain Water","description":"1. Given n non-negative integers representing an elevation map where the width of each bar is 1.\r\n2. Compute how much water it can trap after raining.\r\n","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. n == height.length\r\n2. 0 &lt;= n &lt;= 3 * 10^4\r\n3. 0 &lt;= height[i] &lt;= 10^5","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\n\nint trap(vector<int>& height) {\n \n\n}\n\n\nint main()\n{\n int n;\n cin >> n;\n vector<int>arr(n, 0);\n for (int i = 0; i < n; i++)\n {\n cin >> arr[i];\n }\n cout << trap(arr);\n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static int trap(int[] height) {\r\n return 0;\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int n = Integer.parseInt(read.readLine());\r\n int arr[] = new int[n];\r\n for(int i=0;i<n;i++)arr[i] = Integer.parseInt(read.readLine());\r\n int result = trap(arr);\r\n System.out.println(result);\r\n \r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"12\r\n0\r\n1\r\n0\r\n2\r\n1\r\n0\r\n1\r\n3\r\n2\r\n1\r\n2\r\n1","sampleOutput":"6\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"e679571d-6e14-447b-824e-8f0b0fd80b7c","name":"Trapping Rain Water","slug":"trapping-rain-water","type":1}],"next":{"id":"747b9f03-71da-434d-963b-9914110996ed","name":"Trapping Rain Water Hard MCQ","type":0,"slug":"trapping-rain-water-hard-mcq"},"prev":{"id":"c566a0f1-9323-41c1-8954-951c23a71519","name":"Remove Duplicate Letters","type":3,"slug":"remove-duplicate-letters-9481"}}}`

Editor

# Trapping Rain Water

hard

1. Given n non-negative integers representing an elevation map where the width of each bar is 1. 2. Compute how much water it can trap after raining.

## Constraints

1. n == height.length 2. 0 <= n <= 3 * 10^4 3. 0 <= height[i] <= 10^5

## Format

### Input

Input is managed for you

### Output

Output is managed for you

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}12 0 1 0 2 1 0 1 3 2 1 2 1```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}6 ```

Discussions

Show Discussion

Related Resources