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
dakshbajaj2410in 8 hours
Simple C++ Solution 
public:   long long trappingWater(int arr[], int n){       // code here       long long water = 0;       int left = arr[0];       int right = arr[n-1];       int lIndex = 1;       int rIndex = n-2;       while(lIndex<=rIndex){           if(left<=right){               if(arr[lIndex]<=left){                   water+= left-arr[lIndex];               }else{                   left = arr[lIndex];               }               lIndex++;           }else{               if(arr[rIndex]<=right){                   water+= right-arr[rIndex];               }else{                   right = arr[rIndex];               }               rIndex--;           }       }       return water;   }
0
dakshbajaj2410
This comment was deleted.
0
aditya16jain162 hours ago
 def trappingWater(self, arr,n):       max_ele = arr.index(max(arr))       if (max_ele == n - 1):           thres = arr[0]           water = 0           for i in arr:               if (i >= thres):                   thres = i               else:                   water += thres - i
       elif (max_ele == 0):           arr.reverse()           thres = arr[0]           water = 0           for i in arr:               if (i >= thres):                   thres = i               else:                   water += thres - i
       else:           water = 0           lst = arr[:max_ele + 1]           thres = lst[0]                       for i in lst:               if (i >= thres):                   thres = i               else:                   water += thres - i
           lst = arr[max_ele:]           lst.reverse()           thres = lst[0]           for i in lst:               if (i >= thres):                   thres = i               else:                   water += thres - i       return water    
0
ruchikajamwal03198 hours ago
class Solution{
   // Function to find the trapped water between the blocks.   public:   long long trappingWater(int arr[], int n){       // code here        int lmax[n],rmax[n];      long long int res=0;      lmax[0]=arr[0];      for(int i=1;i<n;i++){          lmax[i]=max(lmax[i-1],arr[i]);      }      rmax[n-1]=arr[n-1];      for(int i=n-2;i>=0;i--){          rmax[i]=max(rmax[i+1],arr[i]);      }      for(int i=1;i<n-1;i++){          res += min(lmax[i],rmax[i])-arr[i];      }      return res;   }};
0
ashishgohil140811 hours ago
def trappingWater(self, a,n):       left = 0       right = n-1       i = 0       j = n-1       vol = 0       while i <= j:           while i <= j and a[i] <= a[j]:               if a[i] <= a[left]:                   vol += a[left] - a[i]                   i += 1               else:                   left = i           while i <= j and a[j] < a[i]:               if a[right] >= a[j]:                   vol += a[right] - a[j]                   j -= 1               else:                   right = j       return vol
0
ashish400rl1 day ago
long long trappingWater(int arr[], int n){       // code here       int lmax[n],rmax[n];       long long int res=0;       lmax[0]=arr[0];       for(int i=1;i<n;i++){           lmax[i]=max(lmax[i-1],arr[i]);       }       rmax[n-1]=arr[n-1];       for(int i=n-2;i>=0;i--){           rmax[i]=max(rmax[i+1],arr[i]);       }       for(int i=1;i<n-1;i++){           res += min(lmax[i],rmax[i])-arr[i];       }       return res;   }
+1
lakshitagaur1231 day ago
Java Solution -:
 
static long trappingWater(int arr[], int n) {        // Your code here       int lmax[] =new int[n];       int rmax[]= new int[n];       lmax[0] = arr[0];       long res=0;       for(int i=1;i<n;i++)       {           lmax[i]= Math.max(arr[i],lmax[i-1]);       }              rmax[n-1]=arr[n-1];       for(int i=n-2;i>=0;i--)       {           rmax[i]= Math.max(arr[i],rmax[i+1]);       }              for(int i=0;i<n;i++)       {           res = res + (Math.min(lmax[i],rmax[i]) - arr[i]);       }       return res;   } } 
0
evgenykalldx2 days ago
    def trappingWater(self, arr,n):       #Code here       left_2_right = {}       right_2_left = {}       max_l2r = max_r2l = trapped_water = 0              for i in range(0,n):          left_2_right[i] = max_l2r = max(arr[i], max_l2r)       for i in range(n-1,-1,-1):          right_2_left[i] = max_r2l = max(arr[i], max_r2l)       for i in range(0,n):          trapped_water += min(left_2_right[i], right_2_left[i]) - arr[i]       return trapped_water
-1
pradhanrahul33462 days ago
static long trappingWater(int arr[], int n) {        // Your code here       int lmax[]=new int[n];       int rmax[]=new int[n];       int res=0;       lmax[0]=arr[0];              for(int i=1;i<n;i++){           lmax[i]=Math.max(arr[i],arr[i-1]);       }       rmax[n-1]=arr[n-1];        for(int i=n-2;i>=0;i--){           rmax[i]=Math.max(arr[i],arr[i+1]);       }       for(int i=0;i<n;i++){           res=res+Math.min(lmax[i],rmax[i])-arr[i];       }       return res;   } 
0
deepakt90su302 days ago
lst=[]s=0n=int(input())a=list(map(int,input().strip().split()))[:n]m=max(a)index=a.index(m)
if(a[0]<=a[n-1]and m==a[n-1]):   t=0   for i in range(t,n):       v=a[t]-a[i]       if(a[t]>a[i]):           x=a[t]-a[i]           lst.append(x)       if(v<0):           t=ielif(a[0]>=a[n-1] and m==a[0]):   t=-1   for i in range(t,-n,-1):       v=a[t]-a[i]       if(a[t]>a[i]):           x=a[t]-a[i]           lst.append(x)       if(v<0):           t=i elif(m>a[0] and m>a[n-1]):   t=0   t1=-1   for i in range(0,index,1):       v=a[t]-a[i]       if(a[t]>a[i]):           x=a[t]-a[i]           lst.append(x)       if(v<0):           t=i   for j in range(t1,-(n-index),-1):       p=a[t1]-a[j]       if(a[t1]>a[j]):           x=a[t1]-a[j]           lst.append(x)       if(p<0):           t1=j   if(v<0):       t1=-1#print(lst)for k in lst:   if(k>0):       s=s+kprint(s)
    
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.