Maximum number of buckets that can be filled | Function to find the maximum number of buckets that can be filled with the amount of water available ; Find the total available water ; Sort the array in ascending order ; Check if bucket can be filled with available water ; Print count of buckets ; Driver code
function getBuckets ( arr , N ) { let availableWater = N * ( N - 1 ) / 2 ; arr . sort ( function ( a , b ) { return a - b } ) ; let i = 0 , sum = 0 ; while ( sum <= availableWater ) { sum += arr [ i ] ; i ++ ; } document . write ( i - 1 ) ; } let arr = [ 1 , 5 , 3 , 4 , 7 , 9 ] ; let N = arr . length ; getBuckets ( arr , N ) ;