📜  可填充的最大桶数

📅  最后修改于: 2021-09-04 08:15:02             🧑  作者: Mango

给定一个由N 个桶的容量组成的数组arr[] ,其中arr[i]表示第i桶的容量。如果可用水的总量是数组索引的总和(基于 1 的索引),则任务是找到可以完全装满可用水的桶的最大数量。

例子:

方法:可以贪婪地解决给定的问题。请按照以下步骤解决给定的问题:

  • 通过计算前N 个自然数的总和来计算总可用水量。
  • 按升序对数组arr[]进行排序。
  • 遍历给定的数组arr[]并找到数组元素的总和,直到该索引,例如i ,其中总和超过总可用性。
  • 完成上述步骤后,打印索引i的值作为可以填充的最大桶数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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


输出:
4

时间复杂度: O(N*log N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live