There are some glasses with equal capacity as 1 litre. The glasses are kept as follows:
1
2 3
4 5 6
7 8 9 10
You can put water to the only top glass. If you put more than 1-litre water to 1st glass, water overflows and fills equally in both 2nd and 3rd glasses. Glass 5 will get water from both 2nd glass and 3rd glass and so on.
If you have X litre of water and you put that water in a top glass, how much water will be contained by the jth glass in an ith row?
Example. If you will put 2 litres on top.
1st – 1 litre
2nd – 1/2 litre
3rd – 1/2 litre

For 2 Liters Water
The approach is similar to Method 2 of the Pascal’s Triangle. If we take a closer look at the problem, the problem boils down to Pascal’s Triangle.
1 ---------------- 1
2 3 ---------------- 2
4 5 6 ------------ 3
7 8 9 10 --------- 4
Each glass contributes to the two glasses down the glass. Initially, we put all water in the first glass. Then we keep 1 litre (or less than 1 litre) in it and move rest of the water to two glasses down to it. We follow the same process for the two glasses and all other glasses till the ith row. There will be i*(i+1)/2 glasses till ith row.
C++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
float findWater( int i, int j, float X)
{
if (j > i)
{
printf ( "Incorrect Inputn" );
exit (0);
}
float glass[i * (i + 1) / 2];
memset (glass, 0, sizeof (glass));
int index = 0;
glass[index] = X;
for ( int row = 1; row <= i ; ++row)
{
for ( int col = 1; col <= row; ++col, ++index)
{
X = glass[index];
glass[index] = (X >= 1.0f) ? 1.0f : X;
X = (X >= 1.0f) ? (X - 1) : 0.0f;
glass[index + row] += X / 2;
glass[index + row + 1] += X / 2;
}
}
return glass[i*(i-1)/2 + j - 1];
}
int main()
{
int i = 2, j = 2;
float X = 2.0;
printf ( "Amount of water in jth glass of ith row is: %f" ,
findWater(i, j, X));
return 0;
}
|
Java
import java.lang.*;
class GFG
{
static float findWater( int i, int j,
float X)
{
if (j > i)
{
System.out.println( "Incorrect Input" );
System.exit( 0 );
}
int ll = Math.round((i * (i + 1 ) ));
float [] glass = new float [ll + 2 ];
int index = 0 ;
glass[index] = X;
for ( int row = 1 ; row <= i ; ++row)
{
for ( int col = 1 ;
col <= row; ++col, ++index)
{
X = glass[index];
glass[index] = (X >= 1 .0f) ? 1 .0f : X;
X = (X >= 1 .0f) ? (X - 1 ) : 0 .0f;
glass[index + row] += X / 2 ;
glass[index + row + 1 ] += X / 2 ;
}
}
return glass[( int )(i * (i - 1 ) /
2 + j - 1 )];
}
public static void main(String[] args)
{
int i = 2 , j = 2 ;
float X = 2 .0f;
System.out.println( "Amount of water in jth " +
"glass of ith row is: " +
findWater(i, j, X));
}
}
|
Python3
def findWater(i, j, X):
if (j > i):
print ( "Incorrect Input" );
return ;
glass = [ 0 ] * int (i * (i + 1 ) / 2 );
index = 0 ;
glass[index] = X;
for row in range ( 1 ,i):
for col in range ( 1 ,row + 1 ):
X = glass[index];
glass[index] = 1.0 if (X > = 1.0 ) else X;
X = (X - 1 ) if (X > = 1.0 ) else 0.0 ;
glass[index + row] + = (X / 2 );
glass[index + row + 1 ] + = (X / 2 );
index + = 1 ;
return glass[ int (i * (i - 1 ) / 2 + j - 1 )];
if __name__ = = "__main__" :
i = 2 ;
j = 2 ;
X = 2.0 ;
res = repr (findWater(i, j, X));
print ( "Amount of water in jth glass of ith row is:" ,res.ljust( 8 , '0' ));
|
C#
using System;
class GFG
{
static float findWater( int i, int j,
float X)
{
if (j > i)
{
Console.WriteLine( "Incorrect Input" );
Environment.Exit(0);
}
int ll = ( int )Math.Round(( double )(i * (i + 1)));
float [] glass = new float [ll + 2];
int index = 0;
glass[index] = X;
for ( int row = 1; row <= i ; ++row)
{
for ( int col = 1;
col <= row; ++col, ++index)
{
X = glass[index];
glass[index] = (X >= 1.0f) ?
1.0f : X;
X = (X >= 1.0f) ? (X - 1) : 0.0f;
glass[index + row] += X / 2;
glass[index + row + 1] += X / 2;
}
}
return glass[( int )(i * (i - 1) /
2 + j - 1)];
}
static void Main()
{
int i = 2, j = 2;
float X = 2.0f;
Console.WriteLine( "Amount of water in jth " +
"glass of ith row is: " +
findWater(i, j, X));
}
}
|
PHP
<?php
function findWater( $i , $j , $X )
{
if ( $j > $i )
{
echo "Incorrect Input\n" ;
return ;
}
$glass = array_fill (0, (int)( $i *
( $i + 1) / 2), 0);
$index = 0;
$glass [ $index ] = $X ;
for ( $row = 1; $row < $i ; ++ $row )
{
for ( $col = 1;
$col <= $row ; ++ $col , ++ $index )
{
$X = $glass [ $index ];
$glass [ $index ] = ( $X >= 1.0) ?
1.0 : $X ;
$X = ( $X >= 1.0) ?
( $X - 1) : 0.0;
$glass [ $index + $row ] += (double)( $X / 2);
$glass [ $index + $row + 1] += (double)( $X / 2);
}
}
return $glass [(int)( $i * ( $i - 1) /
2 + $j - 1)];
}
$i = 2;
$j = 2;
$X = 2.0;
echo "Amount of water in jth " ,
"glass of ith row is: " .
str_pad (findWater( $i , $j ,
$X ), 8, '0' );
?>
|
Javascript
<script>
function findWater(i , j, X)
{
if (j > i)
{
document.write( "Incorrect Input" );
}
var ll = Math.round((i * (i + 1) ));
glass = Array.from({length: ll+2}, (_, i) => 0.0);
var index = 0;
glass[index] = X;
for (row = 1; row <= i ; ++row)
{
for (col = 1;
col <= row; ++col, ++index)
{
X = glass[index];
glass[index] = (X >= 1.0) ? 1.0 : X;
X = (X >= 1.0) ? (X - 1) : 0.0;
glass[index + row] += X / 2;
glass[index + row + 1] += X / 2;
}
}
return glass[parseInt((i * (i - 1) /
2 + j - 1))];
}
var i = 2, j = 2;
var X = 2.0;
document.write( "Amount of water in jth " +
"glass of ith row is: " +
findWater(i, j, X));
</script>
|
Output:
Amount of water in jth glass of ith row is: 0.500000
Time Complexity: O(i*(i+1)/2) or O(i^2)
Auxiliary Space: O(i*(i+1)/2) or O(i^2)
This article is compiled by Rahul and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Method 2 (Using BFS Traversal)
we will discuss another approach to this problem. First, we add a Triplet(row, col,rem-water) in the queue which indicates the starting value of the first element and fills 1-litre water. Then we simply apply bfs i.e. and we add left(row+1, col-1,rem-water) Triplet and right(row+1, col+1,rem-water) Triplet into the queue with half of the remaining water in first Triplet and another half into the next Triplet.
Following is the implementation of this solution.
C++
#include<bits/stdc++.h>
using namespace std;
void findWater( float k, int i, int j)
{
float dp[i+1][2*i + 1];
bool vis[i+1][2*i + 1];
for ( int n=0;n<i+1;n++)
{
for ( int m=0;m<(2*i+1);m++)
{
dp[n][m] = 0;
vis[n][m] = false ;
}
}
queue<pair< int , int >>q;
dp[0][i] = k;
q.push({0,i});
vis[0][i] = true ;
while (!q.empty())
{
pair< int , int >temp = q.front();
q.pop();
int n = temp.first;
int m = temp.second;
if (i == n)
break ;
float x = dp[n][m];
if ( float ((x-1.0)/2.0) < 0)
{
dp[n+1][m-1] += 0;
dp[n+1][m+1] += 0;
}
else
{
dp[n+1][m-1] += float ((x-1.0)/2.0);
dp[n+1][m+1] += float ((x-1.0)/2.0);
}
if (vis[n+1][m-1]== false )
{
q.push({n+1,m-1});
vis[n+1][m-1] = true ;
}
if (vis[n+1][m+1]== false )
{
q.push({n+1,m+1});
vis[n+1][m+1] = true ;
}
}
if (dp[i-1][2*j-1]>1)
dp[i-1][2*j-1] = 1.0;
cout<< "Amount of water in jth glass of ith row is: " ;
cout<<fixed<<setprecision(6)<<dp[i-1][2*j-1]<<endl;
}
int main()
{
float k;
cin>>k;
int i,j;
cin>>i>>j;
findWater(k,i,j);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class Triplet
{
int row;
int col;
double rem_water;
Triplet( int row, int col, double rem_water)
{
this .row=row; this .col=col; this .rem_water=rem_water;
}
}
class GFG
{
public static double findWater( int i, int j, int totalWater)
{
double dp[][] = new double [i+ 1 ][ 2 *i+ 1 ];
Queue<Triplet> queue = new LinkedList<>();
queue.add( new Triplet( 0 ,i,totalWater));
while (!queue.isEmpty())
{
Triplet curr = queue.remove();
if (curr.row == i)
break ;
if (dp[curr.row][curr.col] != 1.0 )
{
double rem_water = 1 -dp[curr.row][curr.col];
if (rem_water > curr.rem_water)
{
dp[curr.row][curr.col] += curr.rem_water;
curr.rem_water = 0 ;
}
else
{
dp[curr.row][curr.col] += rem_water;
curr.rem_water -= rem_water;
}
}
if (curr.rem_water != 0 )
{
queue.add( new Triplet(curr.row + 1 ,curr.col -
1 ,curr.rem_water/ 2.0 ));
queue.add( new Triplet(curr.row + 1 ,curr.col +
1 ,curr.rem_water/ 2.0 ));
}
}
return dp[i- 1 ][ 2 *j- 1 ];
}
public static void main (String[] args)
{
int i = 2 , j = 2 ;
int totalWater = 2 ;
System.out.print( "Amount of water in jth glass of ith row is:" );
System.out.format( "%.6f" , findWater(i, j, totalWater));
}
}
|
Python3
class Triplet:
def __init__( self , row, col, rem_water):
self .row = row
self .col = col
self .rem_water = rem_water
def findWater(i, j, totalWater):
dp = [[ 0.0 for i in range ( 2 * i + 1 )] for j in range (i + 1 )]
queue = []
queue.append(Triplet( 0 ,i,totalWater))
while len (queue) ! = 0 :
curr = queue.pop( 0 )
if curr.row = = i:
break
if dp[curr.row][curr.col] ! = 1.0 :
rem_water = 1 - dp[curr.row][curr.col]
if rem_water > curr.rem_water:
dp[curr.row][curr.col] + = curr.rem_water
curr.rem_water = 0
else :
dp[curr.row][curr.col] + = rem_water
curr.rem_water - = rem_water
if curr.rem_water ! = 0 :
queue.append(Triplet(curr.row + 1 ,curr.col - 1 ,(curr.rem_water / 2 )))
queue.append(Triplet(curr.row + 1 ,curr.col + 1 ,(curr.rem_water / 2 )))
return dp[i - 1 ][ 2 * j - 1 ]
i, j = 2 , 2
totalWater = 2
print ( "Amount of water in jth glass of ith row is:" , end = "")
print ( "{0:.6f}" . format (findWater(i, j, totalWater)))
|
C#
/// of water in j-th glass
using System;
using System.Collections.Generic;
class GFG {
class Triplet {
public int row, col;
public double rem_water;
public Triplet( int row, int col, double rem_water)
{
this .row = row;
this .col = col;
this .rem_water = rem_water;
}
}
public static double findWater( int i, int j, int totalWater)
{
double [,] dp = new double [i+1,2*i+1];
List<Triplet> queue = new List<Triplet>();
queue.Add( new Triplet(0,i,totalWater));
while (queue.Count > 0)
{
Triplet curr = queue[0];
queue.RemoveAt(0);
if (curr.row == i)
break ;
if (dp[curr.row,curr.col] != 1.0)
{
double rem_water = 1-dp[curr.row,curr.col];
if (rem_water > curr.rem_water)
{
dp[curr.row,curr.col] += curr.rem_water;
curr.rem_water = 0;
}
else
{
dp[curr.row,curr.col] += rem_water;
curr.rem_water -= rem_water;
}
}
if (curr.rem_water != 0)
{
queue.Add( new Triplet(curr.row + 1,curr.col -
1,curr.rem_water/2.0));
queue.Add( new Triplet(curr.row + 1,curr.col +
1,curr.rem_water/2.0));
}
}
return dp[i - 1, 2 * j - 1];
}
static void Main() {
int i = 2, j = 2;
int totalWater = 2;
Console.Write( "Amount of water in jth glass of ith row is:" );
Console.Write(findWater(i, j, totalWater).ToString( "0.000000" ));
}
}
|
Javascript
<script>
class Triplet
{
constructor(row,col,rem_water)
{
this .row=row;
this .col=col;
this .rem_water=rem_water;
}
}
function findWater(i,j,totalWater)
{
let dp = new Array(i+1);
for (let k=0;k<dp.length;k++)
{
dp[k]= new Array((2*i)+1);
for (let l=0;l<dp[k].length;l++)
{
dp[k][l]=0;
}
}
let queue = [];
queue.push( new Triplet(0,i,totalWater));
while (queue.length!=0)
{
let curr = queue.shift();
if (curr.row == i)
break ;
if (dp[curr.row][curr.col] != 1.0)
{
let rem_water = 1-dp[curr.row][curr.col];
if (rem_water > curr.rem_water)
{
dp[curr.row][curr.col] += curr.rem_water;
curr.rem_water = 0;
}
else
{
dp[curr.row][curr.col] += rem_water;
curr.rem_water -= rem_water;
}
}
if (curr.rem_water != 0)
{
queue.push( new Triplet(curr.row + 1,curr.col -
1,(curr.rem_water/2)));
queue.push( new Triplet(curr.row + 1,curr.col +
1,(curr.rem_water/2)));
}
}
return dp[i-1][2*j-1];
}
let i = 2, j = 2;
let totalWater = 2;
document.write( "Amount of water in jth glass of ith row is:" );
document.write(findWater(i, j, totalWater).toFixed(6));
</script>
|
Output
Amount of water in jth glass of ith row is:0.500000
Time Complexity: O(i^2)
Auxiliary Space: O(i^2)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 Mar, 2023
Like Article
Save Article