Maximum number of buckets that can be filled
Last Updated :
05 Apr, 2023
Given an array arr[] consisting of capacities of N buckets where arr[i] denotes the capacity of the ith 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 filled with the available water. The bucket will be considered filled if it has at least 1 liter in it.
Examples:
Input: arr[] = {1, 5, 3, 4, 7, 9}
Output: 4
Explanation:
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. in this case it is filled with more than 1 liter so this bucket is also considered.
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:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
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);
}
}
|
Python3
def getBuckets(arr, N) :
availableWater = N * (N - 1 ) / / 2
arr.sort()
i, Sum = 0 , 0
while ( Sum < = availableWater) :
Sum + = arr[i]
i + = 1
print (i - 1 , end = "")
arr = [ 1 , 5 , 3 , 4 , 7 , 9 ]
N = len (arr)
getBuckets(arr, N);
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG
{
static void getBuckets( int [] arr, int N)
{
int availableWater = N * (N - 1) / 2;
Array.Sort(arr);
int i = 0, sum = 0;
while (sum <= availableWater)
{
sum += arr[i];
i++;
}
Console.Write(i - 1);
}
public static void Main(String[] args)
{
int [] arr = { 1, 5, 3, 4, 7, 9 };
int N = arr.Length;
getBuckets(arr, N);
}
}
|
Javascript
<script>
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);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Another Approach:
1. Define a function max_buckets() that takes an array of bucket sizes, the number of buckets, and the amount of liquid as input and returns the maximum number of buckets that can be filled with the given liquid.
2. Initialize two variables filled_buckets and liquid_remaining to 0 and the given liquid, respectively.
3. Iterate through the buckets in descending order of size, starting from the largest bucket size.
4. For each bucket, check if its size is less than or equal to the remaining liquid. If it is, calculate how many times the bucket can be filled with the remaining liquid using integer division and add the result to the filled_buckets variable. Also, update the liquid_remaining variable by subtracting the amount of liquid used to fill the bucket.
5. If the size of the bucket is greater than the remaining liquid, move on to the next bucket.
6. After iterating through all the buckets, return the value of filled_buckets.
7. In the main() function, initialize an array of bucket sizes, the number of buckets, and the amount of liquid.
8.Call the max_buckets() function with the input arguments and store the result in a variable max_filled_buckets.
9.Print the value of max_filled_buckets using printf().
C++
#include <bits/stdc++.h>
using namespace std;
int max_buckets( int buckets[], int n, int liquid) {
int filled_buckets = 0;
int liquid_remaining = liquid;
for ( int i = n-1; i >= 0; i--) {
if (buckets[i] <= liquid_remaining) {
filled_buckets += liquid_remaining / buckets[i];
liquid_remaining %= buckets[i];
}
}
return filled_buckets;
}
int main() {
int buckets[] = {10, 5, 7, 3, 2};
int n = sizeof (buckets) / sizeof (buckets[0]);
int liquid = 20;
int max_filled_buckets = max_buckets(buckets, n, liquid);
cout<< "Maximum filled buckets: " <<max_filled_buckets<<endl;
return 0;
}
|
C
#include <stdio.h>
int max_buckets( int buckets[], int n, int liquid) {
int filled_buckets = 0;
int liquid_remaining = liquid;
for ( int i = n-1; i >= 0; i--) {
if (buckets[i] <= liquid_remaining) {
filled_buckets += liquid_remaining / buckets[i];
liquid_remaining %= buckets[i];
}
}
return filled_buckets;
}
int main() {
int buckets[] = {10, 5, 7, 3, 2};
int n = sizeof (buckets) / sizeof (buckets[0]);
int liquid = 20;
int max_filled_buckets = max_buckets(buckets, n, liquid);
printf ( "Maximum filled buckets: %d\n" , max_filled_buckets);
return 0;
}
|
Java
import java.util.Arrays;
public class GFG
{
public static int maxBuckets( int [] buckets, int n,
int liquid) {
int filledBuckets = 0 ;
int liquidRemaining = liquid;
for ( int i = n- 1 ; i >= 0 ; i--) {
if (buckets[i] <= liquidRemaining) {
filledBuckets
+= liquidRemaining / buckets[i];
liquidRemaining %= buckets[i];
}
}
return filledBuckets;
}
public static void main(String[] args)
{
int [] buckets = { 10 , 5 , 7 , 3 , 2 };
int n = buckets.length;
int liquid = 20 ;
int maxFilledBuckets
= maxBuckets(buckets, n, liquid);
System.out.println( "Maximum filled buckets: "
+ maxFilledBuckets);
}
}
|
Python3
def max_buckets(buckets, n, liquid):
filled_buckets = 0
liquid_remaining = liquid
for i in range (n - 1 , - 1 , - 1 ):
if buckets[i] < = liquid_remaining:
filled_buckets + = liquid_remaining / / buckets[i]
liquid_remaining % = buckets[i]
return filled_buckets
buckets = [ 10 , 5 , 7 , 3 , 2 ]
n = len (buckets)
liquid = 20
max_filled_buckets = max_buckets(buckets, n, liquid)
print ( "Maximum filled buckets: " + str (max_filled_buckets))
|
C#
using System;
class Gfg {
static int max_buckets( int [] buckets, int n, int liquid) {
int filled_buckets = 0;
int liquid_remaining = liquid;
for ( int i = n-1; i >= 0; i--) {
if (buckets[i] <= liquid_remaining) {
filled_buckets += liquid_remaining / buckets[i];
liquid_remaining %= buckets[i];
}
}
return filled_buckets;
}
static void Main() {
int [] buckets = {10, 5, 7, 3, 2};
int n = buckets.Length;
int liquid = 20;
int max_filled_buckets = max_buckets(buckets, n, liquid);
Console.WriteLine( "Maximum filled buckets: " + max_filled_buckets);
}
}
|
Javascript
function max_buckets(buckets, n, liquid) {
let filled_buckets = 0;
let liquid_remaining = liquid;
for (let i = n-1; i >= 0; i--) {
if (buckets[i] <= liquid_remaining) {
filled_buckets += Math.floor(liquid_remaining / buckets[i]);
liquid_remaining %= buckets[i];
}
}
return filled_buckets;
}
let buckets = [10, 5, 7, 3, 2];
let n = buckets.length;
let liquid = 20;
let max_filled_buckets = max_buckets(buckets, n, liquid);
console.log( "Maximum filled buckets: " + max_filled_buckets);
|
Output
Maximum filled buckets: 10
The time complexity of this algorithm is O(nlogn), where n is the number of buckets, due to the use of the modulo operator, which takes logarithmic time. However, in practice, the algorithm is likely to be faster than the previous solution because it avoids the overhead of calling the qsort() function.
The space complexity of this algorithm is O(1), as it only uses a few constant variables.
Share your thoughts in the comments
Please Login to comment...