Given an array **arr[]** consisting of capacities of **N** buckets where **arr[i]** denotes the capacity of the **i ^{th}** bucket. If the total amount of water available is the sum of array indices (

*1-based indexing*), the task is to find the maximum number of buckets that can be fully filled with the available water.

**Examples:**

Input:arr[] = {1, 5, 3, 4, 7, 9}Output:4Explanation:

Total available water = Sum of arrayindices of arr[] = 1 + 2 + 3 + 4 + 5 = 15.

Sorting the array in ascending order modifies the array to {1, 3, 4, 5, 7, 9}.

Fill the bucket having capacity 1, then . Now, available water = 14.

Fill the bucket having capacity 3 . Now, available water = 11.

Fill bucket having capacity 4. Now, available water = 7.

Fill bucket having capacity 5. Now, available water = 2.

Therefore, the total buckets that can be fully filled with water is 4.

Input:arr[] = {2, 5, 8, 3, 2, 10, 8}Output:5

**Approach:** The given problem can be solved Greedily. Follow the steps below to solve the given problem:

- Calculate the total water availability by calculating the sum of first
**N**natural numbers. - Sort the array
**arr[]**in ascending order. - Traverse the given array
**arr[]**and find the sum of array elementsy till that index, say**i**, where the sum exceeds the total availability. - After completing the above steps, print the value of index
**i**as the maximum number of buckets that can be filled.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the maximum number` `// of buckets that can be filled with` `// the amount of water available` `int` `getBuckets(` `int` `arr[], ` `int` `N)` `{` ` ` `// Find the total available water` ` ` `int` `availableWater = N * (N - 1) / 2;` ` ` `// Sort the array in ascending order` ` ` `sort(arr, arr + N);` ` ` `int` `i = 0, sum = 0;` ` ` `// Check if bucket can be` ` ` `// filled with available water` ` ` `while` `(sum <= availableWater) {` ` ` `sum += arr[i];` ` ` `i++;` ` ` `}` ` ` `// Print count of buckets` ` ` `cout << i - 1;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `arr[] = { 1, 5, 3, 4, 7, 9 };` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `getBuckets(arr, N);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.util.*;` `public` `class` `GFG` `{` ` ` ` ` `// Function to find the maximum number` ` ` `// of buckets that can be filled with` ` ` `// the amount of water available` ` ` `static` `void` `getBuckets(` `int` `[] arr, ` `int` `N)` ` ` `{` ` ` `// Find the total available water` ` ` `int` `availableWater = N * (N - ` `1` `) / ` `2` `;` ` ` `// Sort the array in ascending order` ` ` `Arrays.sort(arr);` ` ` `int` `i = ` `0` `, sum = ` `0` `;` ` ` `// Check if bucket can be` ` ` `// filled with available water` ` ` `while` `(sum <= availableWater) {` ` ` `sum += arr[i];` ` ` `i++;` ` ` `}` ` ` `// Print count of buckets` ` ` `System.out.println(i - ` `1` `);` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `[] arr = { ` `1` `, ` `5` `, ` `3` `, ` `4` `, ` `7` `, ` `9` `};` ` ` `int` `N = arr.length;` ` ` `getBuckets(arr, N);` ` ` `}` `}` `// This code is contributed by divyesh072019.` |

## Python3

`# Python3 program for the above approach` `# Function to find the maximum number` `# of buckets that can be filled with` `# the amount of water available` `def` `getBuckets(arr, N) :` ` ` `# Find the total available water` ` ` `availableWater ` `=` `N ` `*` `(N ` `-` `1` `) ` `/` `/` `2` ` ` `# Sort the array in ascending order` ` ` `arr.sort()` ` ` `i, ` `Sum` `=` `0` `, ` `0` ` ` `# Check if bucket can be` ` ` `# filled with available water` ` ` `while` `(` `Sum` `<` `=` `availableWater) :` ` ` `Sum` `+` `=` `arr[i]` ` ` `i ` `+` `=` `1` ` ` `# Print count of buckets` ` ` `print` `(i ` `-` `1` `, end ` `=` `"")` `arr ` `=` `[ ` `1` `, ` `5` `, ` `3` `, ` `4` `, ` `7` `, ` `9` `]` `N ` `=` `len` `(arr)` `getBuckets(arr, N);` `# This code is contributed by divyeshrabadiya07.` |

## C#

`// C# program to implement` `// the above approach` `using` `System;` `using` `System.Collections.Generic;` `using` `System.Linq;` `class` `GFG` `{` ` ` ` ` `// Function to find the maximum number` ` ` `// of buckets that can be filled with` ` ` `// the amount of water available` ` ` `static` `void` `getBuckets(` `int` `[] arr, ` `int` `N)` ` ` `{` ` ` ` ` `// Find the total available water` ` ` `int` `availableWater = N * (N - 1) / 2;` ` ` `// Sort the array in ascending order` ` ` `Array.Sort(arr);` ` ` `int` `i = 0, sum = 0;` ` ` `// Check if bucket can be` ` ` `// filled with available water` ` ` `while` `(sum <= availableWater)` ` ` `{` ` ` `sum += arr[i];` ` ` `i++;` ` ` `}` ` ` `// Print count of buckets` ` ` `Console.Write(i - 1);` ` ` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `[] arr = { 1, 5, 3, 4, 7, 9 };` ` ` `int` `N = arr.Length;` ` ` `getBuckets(arr, N);` `}` `}` `// This code is contributed by splevel62.` |

## Javascript

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

**Output:**

4

**Time Complexity:** O(N*log N)**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**