Maximum number of buckets that can be filled | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getBuckets ( int arr [ ] , int N ) { int availableWater = N * ( N - 1 ) / 2 ; sort ( arr , arr + N ) ; int i = 0 , sum = 0 ; while ( sum <= availableWater ) { sum += arr [ i ] ; i ++ ; } cout << i - 1 ; } int main ( ) { int arr [ ] = { 1 , 5 , 3 , 4 , 7 , 9 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; getBuckets ( arr , N ) ; return 0 ; }