Trapping Rain Water | Practice | GeeksforGeeks

Given an array arr[] of N non-negative integers representing the height of blocks. If width of each block is 1, compute how much water can be trapped between the blocks during the rainy season. 
 
Example 1:
Input:
N = 6
arr[] = {3,0,0,2,0,4}
Output:
10
Explanation: 


Example 2:
Input:
N = 4
arr[] = {7,4,0,9}
Output:
10
Explanation:
Water trapped by above 
block of height 4 is 3 units and above 
block of height 0 is 7 units. So, the 
total unit of water trapped is 10 units.

Example 3:
Input:
N = 3
arr[] = {6,9,9}
Output:
0
Explanation:
No water will be trapped.

Your Task:
You don't need to read input or print anything. The task is to complete the function trappingWater() which takes arr[] and N as input parameters and returns the total amount of water that can be trapped.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Constraints:
3 < N < 106
0 < Ai < 108
0
anuragshubham342 days ago
static long trappingWater(int arr[], int n) {        int i=0,j=n-1,low=arr[0],high=arr[n-1];       long result=0;       while(i<j){           if(arr[i]<arr[j]){               if(arr[i]>low)               {                   low=arr[i];               }               else               {                   result+=low-arr[i];                   i++;               }           }           else{               if(arr[j]>high){                   high=arr[j];               }               else{                   result+=high-arr[j];                   j--;               }           }       }       return result;   } }
0
anuragshubham342 days ago
static long trappingWater(int arr[], int n) {        int[] left=new int[n];       int[] right=new int[n];        left[0]=arr[0];       for(int i=1;i<n;i++){           left[i]=Math.max(arr[i],left[i-1]);       }       right[n-1]=arr[n-1];       for(int j=n-2;j>=0;j--){           right[j]=Math.max(arr[j],right[j+1]);       }       long water=0;       for(int i=0;i<n;i++){           water+=Math.min(left[i],right[i])-arr[i];       }       return water;   } 
0
98rkgautam4 days ago
class Solution{
    
    // arr: input array
    // n: size of array
    // Function to find the trapped water between the blocks.
    static long trappingWater(int arr[], int n) { 
        int i = 0 , j = n-1;
        long totalWater = 0;
        int leftMax = 0 , rightMax = 0;
        while(i <= j){
            leftMax = Math.max(leftMax, arr[i]);
            rightMax = Math.max(rightMax, arr[j]);
            if(leftMax <= rightMax){
                totalWater += leftMax - arr[i];
                i++;
            }else if(leftMax >= rightMax) {
                 totalWater += rightMax - arr[j];
                j--;
            }
        }
        return totalWater;
    } 
}
0
diagovenk4 days ago
Hello guys Easy java solution. Time complexity: O(n), Space complexity O(1). total time: 0.58 sec
 
static long trappingWater(int arr[], int n) {        // Your code here       int leftIndex = 0;       int rightIndex = n-1;       long leftMax = 0;       long rightMax = 0;       long water  = 0;       while(leftIndex<=rightIndex){           if(arr[leftIndex] < arr[rightIndex]){               if(arr[leftIndex] >= leftMax){                   leftMax = arr[leftIndex];                   ++leftIndex;               }               else{                   water += leftMax - arr[leftIndex];                   ++leftIndex;               }           }           else{               if(arr[rightIndex]>= rightMax){                   rightMax = arr[rightIndex];                   --rightIndex;               }               else{                   water += rightMax - arr[rightIndex];                   --rightIndex;               }           }       }              return water;   } 
0
iichipc14 days ago
BLANKED!!
😥
class Solution{
   // Function to find the trapped water between the blocks.   public:   long long trappingWater(int a[], int n){       int l=0, r=n-1;        int n1, n2;        while(l<n-1)       {           if(a[l]!=0 && a[l]>a[l+1])           {               n1 = a[l];               break;            }           else               l++;        }        while(r>0)       {           if(a[r]!=0 && a[r]>a[r-1])           {               n2 = a[r];               break;            }           else               r--;       }              int w = (r-l)-1;       int ans = min(n1,n2)*w;       for(int i=l+1;i<=r-1;i++)       {          if(a[i]!=0)               ans -= a[i];       }              return ans;   }
} ;
 
debug it
 
+2
15shivamk5 days ago
C++ Solution
 
long long trappingWater(int arr[], int n){       // code here       int left[n] = {0};       int right[n] = {0};              left[0] = arr[0];       for(int i=1; i<n; i++){           left[i] = max(arr[i], left[i-1]);       }              right[n-1] = arr[n-1];       for(int j=n-2; j>=0; j--){           right[j] = max(arr[j], right[j+1]);       }              long long int water = 0;       for(int i=0; i<n; i++){           water += min(left[i], right[i]) - arr[i];       }       return water;   }
+1
aniket6518gadhe6 days ago
JAVA SOLUTION 
Total Time Taken:
0.6/1.72
 
static long trappingWater(int arr[], int n) {        int left_max=0,right_max=0,i=0;       long water=0;       n--;       while(i<n){           if(arr[i]<arr[n]){               if(arr[i]>=left_max){                   left_max=arr[i];               }else{                   water+=left_max-arr[i];               }               i++;           }else{               if(arr[n]>=right_max){                   right_max=arr[n];               }else{                   water+=right_max-arr[n];               }               n--;           }       }              return water;   } 
0
putyavka1 week ago
C++ 
TIME COMPLEXITY =O(N) and AUX SPACE =O(1)
No need auxilary space.
class Solution{   public:   long long trappingWater(int arr[], int n){       int i = 0, j = n - 1;       long long sum = 0;       int i_val = 0;       int j_val = 0;       while (i < j) {           while (arr[i] < arr[j]) {               sum += max(0, i_val - arr[i]);               i_val = max(i_val, arr[i]);               i++;           }           while (i < j && arr[i] >= arr[j]) {               sum += max(0, j_val - arr[j]);               j_val = max(j_val, arr[j]);               j--;           }       }       return sum;   }};
0
shuklaakshita101 week ago
C++ SOLUTION
TIME COMPLEXITY =O(N) and AUX SPACE =O(N)
 long long trappingWater(int arr[], int n){       // code here       long long res=0,l=0,r=n-1;       long long max_l=0,max_r=0;       while(l<=r)        {        if(arr[l]<=arr[r]){              if(arr[l]>=max_l)  max_l=arr[l];       else       res+= max_l - arr[l];       l++;       }       else        {       if(arr[r]>= max_r) max_r = arr[r];       else       res+= max_r - arr[r];        r--; }              }       return res;       }
0
lawbindpandey01w1 week ago
Time complexity : O(n) and Aux Space : O(n)
 
long long trappingWater(int arr[], int n){       // code here              int lMax[n];       int rMax[n];              lMax[0] = arr[0];       rMax[n - 1] = arr[n - 1];              for(int i = 1; i < n; i++) lMax[i] = max(lMax[i - 1],arr[i]);       for(int i = n - 2; i >= 0; i--) rMax[i] = max(rMax[i + 1], arr[i]);              long long res = 0;              for(int i = 0; i < n; i++)           res += min(lMax[i], rMax[i]) - arr[i];                     return res;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.