📌  相关文章
📜  一个数组可以分成的最大子集数使得它们的最小值与子集大小的乘积至少为 K

📅  最后修改于: 2021-09-03 14:55:37             🧑  作者: Mango

给定一个由N 个整数和一个整数K组成的数组arr[] ,任务是找到给定数组可以分成的最大不相交子集数,使得每个子集的最小元素与子集至少为K

例子:

方法:根据以下观察,可以贪婪地解决给定的问题:

  • 正如问题陈述中给出的,所形成的子集的最小元素与子集长度的乘积必须至少为 K ,因此为了最大化子集的数量,可以将数组的最大元素分组为子集。
  • 所以思路是将子集的最小元素一一最大化,从而最大化子集的计数。

请按照以下步骤解决问题:

  • 初始化一个变量,比如count0 ,以存储形成的最大子集数。
  • 初始化一个变量,比如length0 ,以存储子集的长度。
  • 按降序对数组进行排序。
  • 遍历给定的数组arr[]并执行以下步骤:
    • length的值增加1
    • 如果(arr[i] * length)的值大于K ,则将count的值增加1并将length的值更新为0
  • 完成上述步骤后,打印count的值作为最终形成的最大子集数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum number
// of subsets possible such that
// product of their minimums and the
// size of subsets are at least K
int maximumSubset(int arr[], int N,
                  int K)
{
    // Sort the array in
    // descending order
    sort(arr, arr + N, greater());
 
    // Stores the size of
    // the current subset
    int len = 0;
 
    // Stores the count of subsets
    int ans = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        // Increment length of the
        // subsets by 1
        len++;
 
        // If arr[i] * len >= K
        if (arr[i] * len >= K) {
 
            // Increment ans by one
            ans++;
 
            // Update len
            len = 0;
        }
    }
 
    // Return the maximum possible
    // subsets formed
    return ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 7, 11, 2, 9, 5 };
    int K = 10;
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << maximumSubset(arr, N, K);
 
    return 0;
}


Java
import java.util.*;
public class GFG
{
 
  // Function to reverse the sorted array
  public static void reverse(int[] arr)
  {
 
    // Length of the array
    int n = arr.length;
 
    // Swaping the first half elements with last half
    // elements
    for (int i = 0; i < n / 2; i++) {
 
      // Storing the first half elements temporarily
      int temp = arr[i];
 
      // Assigning the first half to the last half
      arr[i] = arr[n - i - 1];
 
      // Assigning the last half to the first half
      arr[n - i - 1] = temp;
    }
  }
 
  // Function to find the maximum number
  // of subsets possible such that
  // product of their minimums and the
  // size of subsets are at least K
  public static int maximumSubset(int arr[], int N, int K)
  {
     
    // Sort the array in
    // descending order
    Arrays.sort(arr);
    reverse(arr);
    // Stores the size of
    // the current subset
    int len = 0;
 
    // Stores the count of subsets
    int ans = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
      // Increment length of the
      // subsets by 1
      len++;
 
      // If arr[i] * len >= K
      if (arr[i] * len >= K) {
 
        // Increment ans by one
        ans++;
 
        // Update len
        len = 0;
      }
    }
 
    // Return the maximum possible
    // subsets formed
    return ans;
  }
 
  // Driver Code
  public static void main(String args[]) {
    int arr[] = { 7, 11, 2, 9, 5 };
    int K = 10;
    int N =arr.length;
    System.out.println(maximumSubset(arr, N, K));
  }
}
 
// This code is contributed by SoumikMondal


Python3
# Python 3 program for the above approach
 
# Function to find the maximum number
# of subsets possible such that
# product of their minimums and the
# size of subsets are at least K
def maximumSubset(arr, N,
                  K):
 
    # Sort the array in
    # descending order
    arr.sort(reverse = True)
 
    # Stores the size of
    # the current subset
    len = 0
 
    # Stores the count of subsets
    ans = 0
 
    # Traverse the array arr[]
    for i in range(N):
 
        # Increment length of the
        # subsets by 1
        len += 1
 
        # If arr[i] * len >= K
        if (arr[i] * len >= K):
 
            # Increment ans by one
            ans += 1
 
            # Update len
            len = 0
 
    # Return the maximum possible
    # subsets formed
    return ans
 
# Driver Code
if __name__ == "__main__":
 
    arr = [7, 11, 2, 9, 5]
    K = 10
    N = len(arr)
    print(maximumSubset(arr, N, K))
 
    # This code is contributed by ukasp.


C#
using System;
 
public class GFG
{
 
    // Function to reverse the sorted array
    public static void reverse(int[] arr)
    {
 
        // Length of the array
        int n = arr.Length;
 
        // Swaping the first half elements with last half
        // elements
        for (int i = 0; i < n / 2; i++) {
 
            // Storing the first half elements temporarily
            int temp = arr[i];
 
            // Assigning the first half to the last half
            arr[i] = arr[n - i - 1];
 
            // Assigning the last half to the first half
            arr[n - i - 1] = temp;
        }
    }
 
    // Function to find the maximum number
    // of subsets possible such that
    // product of their minimums and the
    // size of subsets are at least K
    public static int maximumSubset(int []arr, int N, int K)
    {
 
        // Sort the array in
        // descending order
        Array.Sort(arr);
        reverse(arr);
       
        // Stores the size of
        // the current subset
        int len = 0;
 
        // Stores the count of subsets
        int ans = 0;
 
        // Traverse the array []arr
        for (int i = 0; i < N; i++)
        {
 
            // Increment length of the
            // subsets by 1
            len++;
 
            // If arr[i] * len >= K
            if (arr[i] * len >= K)
            {
 
                // Increment ans by one
                ans++;
 
                // Update len
                len = 0;
            }
        }
 
        // Return the maximum possible
        // subsets formed
        return ans;
    }
 
    // Driver Code
    public static void Main(String []args)
    {
        int []arr = { 7, 11, 2, 9, 5 };
        int K = 10;
        int N = arr.Length;
        Console.WriteLine(maximumSubset(arr, N, K));
    }
}
 
// This code is contributed by aashish1995.


Javascript


输出:
2

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

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