Water Overflow | Practice | GeeksforGeeks

There is a stack of water glasses in a form of pascal triangle and a person wants to pour the water at the topmost glass, but the capacity of each glass is 1 unit. Overflow takes place in such a way that after 1 unit, 1/2 of remaining unit gets into bottom left glass and other half in bottom right glass.Now John pours K units of water in the topmost glass and wants to know how much water is there in the Cth glass of the Rth row.
Note: Assume that there are enough glasses in the triangle till no glass overflows.
 
Example 1:
Input:
K = 3, R = 2, C = 1
Output:
1.000000
Explanation:
After the first glass, 2 units of water
will remain and they will spread equally
on the two glasses on the second row.
Therefore, the glass on the 2nd row and
1st column will have 1 unit of water.
Example 2:
Input:
K = 3, R = 2, C = 2
Output:
1.000000
Explanation:
After the first glass, 2 units of water
will remain and they will spread equally
on the two glasses on the second row.
Therefore, the glass on the 2nd row and
2nd column will have 1 unit of water.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function waterOverflow() which takes 3 Integers K, R and C as input and returns the amount of water(6 digits of precision) in the Cth glass of Rth row.
 
Expected Time Complexity: O(K2)
Expected Auxiliary Space: O(K2)
 
Constraints:
1 <= K <= 500
1 <= R,C <= K
+2
prasadkandekar5552 months ago
C++ Solution :)
The Approach is Simple we have** stack Glasses in a pyramid**.
Here first row has one glass and the second row has 2 glasses, and so on to until 100th row.
some water is poured into the first glass at the top.
As , it got fulled the excess liquid get poured to remaining glasses.
When those glasses become full, any excess water will fall equally to the left and right of those glasses, and so on.
So if the first glass is full then only remaining glasses get poured.
So iterating over row.
now assume first glass when got filled so it overflow is divided equally in the 2 glasses which were at next level.
so assume 1 unit is the limit of top glass it can hold.
Therefore, the next level will got (v[i][j] - 1) / 2.0 as equal amount will be stored at next level.
At last we will return v[row][glass] the answer.
class Solution {
public:
    double waterOverflow(int poured, int row, int glass) {
        double v[101][101] = {0.0};
        v[0][0] = poured;
        
        for(int i = 0; i < 100; ++i) {
            for(int j = 0; j <= i; ++j) {
                double q = (v[i][j] - 1) / 2.0;
                // glass is >= 1
                if(v[i][j] >= 1) {
                    // split  (v[i][j] - 1) into next level
                    v[i+1][j] += q;
                    v[i+1][j+1] += q;
                    v[i][j] = 1;
                }
            }
        }
        return v[row-1][glass-1];
    }
};
0
rehasreekoneru4 months ago
double waterOverflow(int k, int R, int C) {
     double dp[k+1][k+1];
     memset(dp,0,sizeof(dp));
     dp[0][0] = k;
     for(int i=0;i<k+1;i++){
         for(int j=0;j<k+1;j++){
             if(dp[i][j]>1){
                 double dummy = dp[i][j]-1;
                 dp[i][j] = 1;
                 dummy = double(dummy/2);
                 dp[i+1][j]+=dummy;
                 dp[i+1][j+1]+=dummy;
             }
         }
     }
     return dp[R-1][C-1];  
   }
0
debjyotichattopadhyay4 months ago
//JAVA top down dp (recursive) with explanation

class Solution {
    public double waterOverflow(int K, int R, int C) {
        double store[][]=new double[K+1][K+1];
        for(int i=0;i<=K;i++){
            for(int j=0;j<=K;j++){
                store[i][j]=-1;
            }
        }
        
        if(R==1)
            return(K>=1?1:K);
        
        if((dp(K,R-1,C-1,store)+dp(K,R-1,C,store))/2>=1)
            return(1);
        return((dp(K,R-1,C-1,store)+dp(K,R-1,C,store))/2);
    }//waterOverFlow
    public double dp(int k,int r,int c,double store[][]){//returns the total amount of water overflow from r,c glass
        if(c==0 || r==0 ){
            store[r][c]=0;
            return(0);
        }
            
        if(r==1 && c==1){
            if(k<=1)
                store[r][c]=0;
            else
                store[r][c]=k-1;
            return(store[r][c]);
        }
            
        if(store[r][c]!=-1)
            return(store[r][c]);
        
        double v1=dp(k,r-1,c-1,store)/2.0;//overflow from top left
        double v2=dp(k,r-1,c,store)/2.0;//overflow from top right
        
        if(v1+v2<=1)//overflow from above two glass is <=1 so no over flow will occur in the current glass
            store[r][c]=0;
        else   //overflow is more than 1 so excess water will overflow
            store[r][c]=v1+v2-1;
        return(store[r][c]);
    }//overFlow
}//Solution
-2
harshc4668 months ago
Can anyone tell me what is wrong with this code:
double waterOverflow(int K, int R, int C) {
       double dp[K+1][K+1];
       dp[0][0]=K;
       for(int i=0; i<=K; i++){
           for(int j=0; j<=i;j++){
               if(dp[i][j]>1.0){
                   int spare = dp[i][j]-1;
                   dp[i][j]=1.0;
                   dp[i+1][j]=spare/2.0;
                   dp[i+1][j+1]=spare/2.0;
               }
           }
       }
       return dp[R-1][C-1];
   }
 
0
Harsh Kesharwani9 months ago
Harsh Kesharwani
double waterOverflow(int K, int R, int C)     {        double DP[R][R];                memset(DP,0.0,sizeof(DP));                DP[0][0] = K;                for(int i=0;i<r-1;i++) {="" for(int="" j="0;j&lt;R-1;j++)" {="" if(dp[i][j]="">1)                {                            double left = (double)DP[i][j]-1;                            DP[i][j] = 1;                            DP[i+1][j] += (double)left/2;                            DP[i+1][j+1] += (double)left/2;                        }                    }                }        if(DP[R-1][C-1]>1)             return 1;                return DP[R-1][C-1];    }
0
Sarath Mohan9 months ago
Sarath Mohan
Easy Solution C++: https://uploads.disquscdn.c...
0
HUNTER10 months ago
HUNTER
Create a matrix of size R*R, and consider only lower triangle matrix.Water which comes to a glass would be, half of the overflow water in the upper glass, which is at left and right to the glass.
if(dp[i-1][j-1]> 1.0)            dp[i][j] = (dp[i-1][j-1]-1)/2;
 if(dp[i-1][j]> 1.0)            dp[i][j] += (dp[i-1][j]-1)/2;
+3
Debarshi Ghosh11 months ago
Debarshi Ghosh
Please fix the judge for this one(java). I am getting WA because my output is 1.0 in place of 1.
0
rian😎11 months ago
rian😎
C++ code Execution time 0.06 secshttps://uploads.disquscdn.c...
0
Akash Kumar Seth1 year ago
Akash Kumar Seth
Got multiple error just because of output format.Working output format for python.return "{:.6f}".format(matrix[R][C]).rstrip('0').rstrip('.')
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.