Program to find amount of water in a given glass | class Triplet which stores curr row curr col and curr rem - water of every glass ; Returns the amount of water in jth glass of ith row ; stores how much amount of water present in every glass ; store Triplet i . e curr - row , curr - col , rem - water ; we take the center of the first row for the location of the first glass ; this while loop runs unless the queue is not empty ; First we remove the Triplet from the queue ; as we know we have to calculate the amount of water in jth glass of ith row . so first we check if we find solutions for the the every glass of i 'th row.  so, if we have calculated the result  then we break the loop  and return our answer ; As we know maximum capacity of every glass is 1 unit . so first we check the capacity of curr glass is full or not . ; calculate the remaining capacity of water for curr glass ; if the remaining capacity of water for curr glass is greater than then the remaining capacity of tatal water then we put all remaining water into curr glass ; if remaining capacity of tatal water is not equal to zero then we add left and right glass of the next row and gives half capacity of tatal water to both the glass ; return the result for jth glass of ith row ; Driver Code ; Total amount of water
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 ( " " ) ; document . write ( findWater ( i , j , totalWater ) . toFixed ( 6 ) ) ;