Water Collection | Practice | GeeksforGeeks

It is raining in Geek City. The height of the buildings in the city is given in an array. Calculate the amount of water  that can be collected between all the buildings.
 
Example 1:
Input: 
N = 5
Arr[] = {3, 0, 2, 0, 4}
Output: 7
Explanation:
Geek city looks like

We can trap "3 units" of water between
3 and 2, "1 unit" on top of bar 2 and
"3 units" between 2  and 4. 
 
Example 2:
Input: 
N = 12
Arr[] = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Explanation:
The structure is like below

Trap "1 unit" between first 1 and 2,
"4 units" between first 2 and 3 and
"1 unit" between second last 1 and last 2 

 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function maxWater() which takes the array of integers arr[] and n as input parameters and returns the amount of water collected.


Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ N ≤ 105
1 ≤ Arr[i] ≤ 103
 
0
dangrio1 month ago
int left=0, right=n-1;           int water=0, maxLeft=0, maxRight=0;           while(left<=right){               if(arr[left]<=arr[right]){                   if(arr[left]>=maxLeft) maxLeft = arr[left];                   else water += maxLeft-arr[left];                   left++;               }else{                   if(arr[right]>=maxRight) maxRight = arr[right];                   else water += maxRight-arr[right];                   right--;               }           }           return water;
0
akkeshri140420013 months ago
int maxWater(int arr[], int n) {           // code here            vector<int>left(n,0),right(n,0);           left[0]=arr[0];           right[n-1]=arr[n-1];           for(int i=1;i<=n-1;i++){               left[i]=max(left[i-1],arr[i]);               right[n-1-i]=max(right[n-i],arr[n-1-i]);           }           int water=0;           for(int i=0;i<n;i++){               water+=min(left[i],right[i])-arr[i];           }           return water;              }
 
0
vishalja77193 months ago
/////c++ code
int left=0,right=n-1;  int result=0,maxleft=0,maxright=0;  while(left <= right){      if(arr[left] <= arr[right])////      {          if(arr[left] >= maxleft)          {              maxleft=arr[left];          }          else          {             result=result+maxleft-arr[left];           }          left++;      }      else      {           if(arr[right] >= maxright)           {               maxright=arr[right];           }           else           {               result=result+maxright-arr[right];           }           right--;      }  }  return result;   }   
0
arpita biswal3 months ago
Time taken:0.7/3.8
Javascript
 
class Solution {   int maxWater(int arr[], int n) {       // code here       int leftmax=arr[0],rightmax=arr[n-1];       int mina[]=new int[n];       int maxa[] = new int[n];       mina[0]=leftmax;       mina[n-1]=rightmax;       maxa[0]=leftmax;       maxa[n-1]=rightmax;            for(int i=1;i<n-1;i++){ if(arr[i]>leftmax){     mina[i]=arr[i];     leftmax=arr[i]; } else{    mina[i]=leftmax; }}
for(int i=n-2;i>=1;i--){ if(arr[i]>rightmax){     maxa[i]=arr[i];     rightmax=arr[i]; } else{    maxa[i]=rightmax; }}int trappedwater=0;for(int i=1;i<n-1;i++){ int waterlevel = Math.min(mina[i-1],maxa[i+1]); int waterCollect = (waterlevel-arr[i]); if(waterCollect >0){   trappedwater=trappedwater+waterCollect; } }return trappedwater;}}
0
paulshankhdeep3 months ago
SIMPLE JAVA SOLUTION
 
class Solution {   int maxWater(int arr[], int n) {       // code here       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 i=n-2; i>=0; i--) {           right[i] = Math.max(arr[i],right[i+1]);       }        int ans = 0;       for (int i = 0; i<n; i++) {           ans = ans + (Math.min(left[i],right[i])-arr[i]);       }       return ans;   }}
0
tahabasra925 months ago
this one is same as trapping rain water problem available on gfg 
 
time required  0.2
 
class Solution {public:   int maxWater(int a[], int n) {             int left[n];     int right[n];          left[0]=a[0];     for(int i=1;i<n;i++){         left[i]=max(a[i],left[i-1]);     }          right[n-1]=a[n-1];          for(int i=n-2;i>=0;i--){         right[i]=max(a[i],right[i+1]);     }          long int ans=0;     for(int i=0;i<n;i++){         ans=ans+(min(left[i],right[i])-a[i]);     }          return ans;    }};
 
 
0
imranwahid6 months ago
Easy C++ solution
0
wajid945716 months ago
int maxWater(int arr[], int n){   int ar1[n];   int ar2[n];
   int wt = 0;   ar1[0] = arr[0];   for (int i = 1; i < n; i++)       ar1[i] = max(ar1[i - 1], arr[i]);   ar2[n - 1] = arr[n - 1];   for (int i = n - 2; i >= 0; i--)       ar2[i] = max(ar2[i + 1], arr[i]);
   for (int i = 0; i < n; i++)       wt += min(ar1[i], ar2[i]) - arr[i];   return wt;}
0
mohdabuzaid0132
This comment was deleted.
0
Mohit Kesharwani8 months ago
Mohit Kesharwani
class Solution {public:    int maxWater(int arr[], int n) {            int sum=0;            int left[n]={0};            int right[n]={0};
             left[0]=arr[0];            for(int i=1;i<n;i++){ left[i]="{max(left[i-1],arr[i])};" }="" right[n-1]="arr[n-1];" for(int="" i="n-2;i">=0;i--){                 right[i]={max(right[i+1],arr[i])};              }
            for(int i=0;i<n;i++){ sum="sum+(min(left[i],right[i])-arr[i]);" }="" return="" sum;="" }="">
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.