Maximum number of buckets that can be filled | Java program for the above approach ; 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
import java . util . * ; public class GFG { static void getBuckets ( int [ ] arr , int N ) { int availableWater = N * ( N - 1 ) / 2 ; Arrays . sort ( arr ) ; int i = 0 , sum = 0 ; while ( sum <= availableWater ) { sum += arr [ i ] ; i ++ ; } System . out . println ( i - 1 ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 3 , 4 , 7 , 9 } ; int N = arr . length ; getBuckets ( arr , N ) ; } }