📌  相关文章
📜  最大化具有最小元素乘积和子集大小至少 X 的子集计数

📅  最后修改于: 2021-09-02 06:31:31             🧑  作者: Mango

给定一个由N 个整数和一个整数X组成的数组arr[] ,任务是计算给定数组中可能的最大子集数

例子:

方法:该问题可以使用贪心方法解决。思路是将数组按降序排序,然后逐个遍历元素,检查所需条件。
请按照以下步骤解决问题。

  1. 按降序对数组进行排序。
  2. 初始化变量countersz以存储可能子集的计数以及当前子集的大小。
  3. 迭代给定的数组并检查arr[i] * sz ≥ X是否。如果发现为真,则重置sz并将计数器加 1。
  4. 最后,在完成数组的遍历后,打印counter作为所需的答案。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Comparator function to return
// the greater of two numbers
bool comp(int a, int b)
{
    return a > b;
}
 
// Function to return the maximum count
// of subsets possible which
// satisy the above condition
int maxSubset(int arr[], int N, int X)
{
 
    // Sort the array in
    // descending order
    sort(arr, arr + N, comp);
 
    // Stores the count of subsets
    int counter = 0;
 
    // Stores the size of
    // the current subset
    int sz = 0;
 
    for (int i = 0; i < N; i++)
    {
        sz++;
 
        // Check for the necessary
        // conditions
        if (arr[i] * sz >= X) {
            counter++;
            sz = 0;
        }
    }
    return counter;
}
 
// Driver Code
int main()
{
    int arr[] = { 7, 11, 2, 9, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int X = 10;
    cout << maxSubset(arr, N, X);
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to return the maximum count
// of subsets possible which
// satisy the above condition
static int maxSubset(Integer arr[], int N,
                                    int X)
{
     
    // Sort the array in
    // descending order
    Arrays.sort(arr, Collections.reverseOrder());
 
    // Stores the count of subsets
    int counter = 0;
 
    // Stores the size of
    // the current subset
    int sz = 0;
 
    for(int i = 0; i < N; i++)
    {
        sz++;
 
        // Check for the necessary
        // conditions
        if (arr[i] * sz >= X)
        {
            counter++;
            sz = 0;
        }
    }
    return counter;
}
 
// Driver Code
public static void main(String[] args)
{
    Integer arr[] = { 7, 11, 2, 9, 5 };
    int N = arr.length;
    int X = 10;
     
    System.out.print(maxSubset(arr, N, X));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 Program to implement
# the above approach
 
# Function to return the maximum count
# of subsets possible which
# satisy the above condition
def maxSubset(arr, N, X):
 
    # Sort the array in
    # descending order
    arr.sort(reverse = True)
 
    # Stores the count of subsets
    counter = 0
 
    # Stores the size of
    # the current subset
    sz = 0
 
    for i in range(N):
        sz += 1
 
        # Check for the necessary
        # conditions
        if(arr[i] * sz >= X):
            counter += 1
            sz = 0
 
    return counter
 
# Driver Code
 
# Given array
arr = [ 7, 11, 2, 9, 5 ]
N = len(arr)
X = 10
 
# Function call
print(maxSubset(arr, N, X))
 
# This code is contributed by Shivam Singh


C#
// C# program to implement
// the above approach
using System;
using System.Linq;
 
class GFG{
 
// Function to return the maximum count
// of subsets possible which
// satisy the above condition
static int maxSubset(int []arr, int N,
                                int X)
{
     
    // Sort the array in
    // descending order
    Array.Sort(arr);
    Array.Reverse(arr);
 
    // Stores the count of subsets
    int counter = 0;
 
    // Stores the size of
    // the current subset
    int sz = 0;
 
    for(int i = 0; i < N; i++)
    {
        sz++;
 
        // Check for the necessary
        // conditions
        if (arr[i] * sz >= X)
        {
            counter++;
            sz = 0;
        }
    }
    return counter;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 7, 11, 2, 9, 5 };
    int N = arr.Length;
    int X = 10;
     
    Console.Write(maxSubset(arr, N, X));
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
2

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

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