📌  相关文章
📜  最大化要形成的群,使得群的大小与其最小元素的乘积至少为 K

📅  最后修改于: 2022-05-13 01:56:07.019000             🧑  作者: Mango

最大化要形成的群,使得群的大小与其最小元素的乘积至少为 K

给定一个数组,长度为Narr[]和一个整数K。i 个元素的值为arr[i] 。任务是找到最大组数,使得对于每个组,该组中元素数与最小元素的乘积至少为K。

例子:

方法:给定的问题可以通过贪心方法来解决。为了最大化组的数量,对数组进行排序并开始创建组,首先取较大的元素,因为这将帮助我们最大化每个组的最小元素。因此,每组所需的元素数量将减少,我们将最大化组数。请按照以下步骤解决问题:

  • 按升序对给定数组进行排序。
  • 将变量anscount分别初始化为01ans将存储可以创建的组的总数, count将存储当前组的大小。
  • 使用变量i[N-1 到 0]遍历给定数组并执行以下步骤:
    • 如果arr[i] (当前组的最小元素)和 count(当前组的大小)的乘积大于等于k ,则将count (组的总数)增加1并将计数初始化为1。
    • 否则,将当前组中的元素数量增加1
  • 完成这些步骤后,打印ans的值作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to return the maximum number
// of groups that can be formed from given array
int maximumgroups(vector& arr, int N, int k)
{
 
    // Sorting the given array in increasing order
    sort(arr.begin(), arr.end());
 
    int ans = 0, count = 1;
 
    // Start creating the groups by taking
    // the bigger elements first because this
    // will help us in maximizing our
    // minimum element of each group
    for (int i = N - 1; i >= 0; i--) {
 
        // If the product of minimum element of
        // current group and count is greater equal
        // to k then increase the ans by one and
        // initialize the count to 1
        if (arr[i] * count >= k) {
            ans++;
            count = 1;
        }
        // Otherwise increase the number of elements
        // in the current group by one
        else {
            count++;
        }
    }
 
    // Return the maximum number of groups possible
    return ans;
}
 
// Driver Code
int main()
{
    int N = 5;
    int k = 10;
    vector arr = { 7, 11, 2, 9, 5 };
    int res = maximumgroups(arr, N, k);
 
    cout << res << endl;
 
    return 0;
}


Java
// Java program for the above approach
import java.util.Arrays;
 
class GFG {
 
  // Function to return the maximum number
  // of groups that can be formed from given array
  public static int maximumgroups(int[] arr, int N, int k) {
 
    // Sorting the given array in increasing order
    Arrays.sort(arr);
 
    int ans = 0, count = 1;
 
    // Start creating the groups by taking
    // the bigger elements first because this
    // will help us in maximizing our
    // minimum element of each group
    for (int i = N - 1; i >= 0; i--) {
 
      // If the product of minimum element of
      // current group and count is greater equal
      // to k then increase the ans by one and
      // initialize the count to 1
      if (arr[i] * count >= k) {
        ans++;
        count = 1;
      }
      // Otherwise increase the number of elements
      // in the current group by one
      else {
        count++;
      }
    }
 
    // Return the maximum number of groups possible
    return ans;
  }
 
  // Driver Code
  public static void main(String args[]) {
    int N = 5;
    int k = 10;
    int[] arr = { 7, 11, 2, 9, 5 };
    int res = maximumgroups(arr, N, k);
 
    System.out.println(res);
  }
}
 
// This code is contributed by saurabh_jaiswal.


Python3
# Python 3 program for the above approach
 
# Function to return the maximum number
# of groups that can be formed from given array
def maximumgroups(arr, N, k):
 
    # Sorting the given array in increasing order
    arr.sort();
    ans = 0
    count = 1;
 
    # Start creating the groups by taking
    # the bigger elements first because this
    # will help us in maximizing our
    # minimum element of each group
    for i in range(N - 1, -1, -1):
 
        # If the product of minimum element of
        # current group and count is greater equal
        # to k then increase the ans by one and
        # initialize the count to 1
        if (arr[i] * count >= k):
            ans += 1
            count = 1;
 
        # Otherwise increase the number of elements
        # in the current group by one
        else:
            count += 1
   
    # Return the maximum number of groups possible
    return ans;
 
# Driver Code
if __name__ == "__main__":
   
    N = 5;
    k = 10;
    arr = [ 7, 11, 2, 9, 5 ];
    res = maximumgroups(arr, N, k);
 
    print(res );
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
 
public class GFG {
 
  // Function to return the maximum number
  // of groups that can be formed from given array
  public static int maximumgroups(int[] arr, int N, int k) {
 
    // Sorting the given array in increasing order
    Array.Sort(arr);
 
    int ans = 0, count = 1;
 
    // Start creating the groups by taking
    // the bigger elements first because this
    // will help us in maximizing our
    // minimum element of each group
    for (int i = N - 1; i >= 0; i--) {
 
      // If the product of minimum element of
      // current group and count is greater equal
      // to k then increase the ans by one and
      // initialize the count to 1
      if (arr[i] * count >= k) {
        ans++;
        count = 1;
      }
      // Otherwise increase the number of elements
      // in the current group by one
      else {
        count++;
      }
    }
 
    // Return the maximum number of groups possible
    return ans;
  }
 
  // Driver Code
  public static void Main(String []args) {
    int N = 5;
    int k = 10;
    int[] arr = { 7, 11, 2, 9, 5 };
    int res = maximumgroups(arr, N, k);
 
    Console.WriteLine(res);
  }
}
 
// This code is contributed by 29AjayKumar


Javascript



输出
2

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