📌  相关文章
📜  将数组拆分为最大可能的子集,其长度与最大元素的乘积至少为 K

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

将数组拆分为最大可能的子集,其长度与最大元素的乘积至少为 K

给定一个由N个整数和一个正整数K组成的数组arr[] ,任务是通过将数组元素划分为不相交的子集来最大化其大小与其最大元素的乘积至少为K的子集的数量。

例子:

方法:给定的问题可以通过观察至少 K的元素可以单独形成一个合适的群这一事实来解决。为了提取元素不大于等于K的最大组数,想法是从数组arr[]的最小元素开始包含元素,然后继续移动到更大的元素,直到找到合适的组形成。请按照以下步骤解决问题:

  • 初始化一个变量,比如count0 ,它存储子集的结果计数, pre1来存储组的初始大小。
  • 按升序对给定数组arr[]进行排序。
  • 使用变量i遍历范围[0, N]并执行以下步骤:
    • 如果arr[i] * pre的值至少为 K ,则将count的值增加1并将pre的值设置为1
    • 否则,将pre的值增加1
  • 执行上述步骤后,将count的值打印为子集的结果计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum
// number of subsets into which
// the array can be split
void countOfSubsets(int arr[], int N,
                    int K)
{
    // Stores the maximum number of
    // subsets
    int count = 0;
    int pre = 1;
 
    // Sort the given array
    sort(arr, arr + N);
 
    // Iterate over the range [0, N]
    for (int i = 0; i < N; i++) {
 
        // If current subset satisfies
        // the given condition
        if (arr[i] * pre >= K) {
            count++;
            pre = 1;
        }
 
        // Otherwise, increment pre
        else {
            pre++;
        }
    }
 
    cout << count;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 5, 4, 2, 3 };
    int K = 2;
    int N = sizeof(arr) / sizeof(arr[0]);
 
    countOfSubsets(arr, N, K);
 
    return 0;
}


Java
/*package whatever //do not write package name here */
 
import java.util.*;
 
class GFG {
    // Function to find the maximum
    // number of subsets into which
    // the array can be split
    public static void countOfSubsets(int[] arr, int N,
                                      int K)
    {
        // Stores the maximum number of
        // subsets
        int count = 0;
        int pre = 1;
 
        // Sort the given array
        Arrays.sort(arr);
 
        // Iterate over the range [0, N]
        for (int i = 0; i < N; i++) {
 
            // If current subset satisfies
            // the given condition
            if (arr[i] * pre >= K) {
                count++;
                pre = 1;
            }
 
            // Otherwise, increment pre
            else {
                pre++;
            }
        }
 
        System.out.println(count);
    }
 
    public static void main(String[] args)
    {
        int[] arr = { 1, 5, 4, 2, 3 };
        int K = 2;
        int N = 5;
 
        countOfSubsets(arr, N, K);
    }
}


Python3
# Function to find the maximum
# number of subsets into which
# the array can be split
def countOfSubsets(arr, N, K):
   
    # Stores the maximum number of
    # subsets
    count = 0
    pre = 1
     
    # Sort the given array
    arr.sort()
     
    # Iterate over the range [0, N]
    for i in range(N):
       
        # If current subset satisfies
        # the given condition
        if arr[i] * pre >= K:
            count = count + 1
            pre = 1
             
        # Otherwise, increment pre
        else:
            pre = pre + 1
    print(count)
 
# Driver code
arr = [1, 5, 4, 2, 3]
N = 5
K = 2
countOfSubsets(arr, N, K)
 
# This code is contributed by maddler.


C#
// C# program for the above approach
 
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the maximum
// number of subsets into which
// the array can be split
static void countOfSubsets(int []arr, int N,
                    int K)
{
    // Stores the maximum number of
    // subsets
    int count = 0;
    int pre = 1;
 
    // Sort the given array
    Array.Sort(arr);
 
    // Iterate over the range [0, N]
    for (int i = 0; i < N; i++) {
 
        // If current subset satisfies
        // the given condition
        if (arr[i] * pre >= K) {
            count++;
            pre = 1;
        }
 
        // Otherwise, increment pre
        else {
            pre++;
        }
    }
 
    Console.Write(count);
}
 
// Driver Code
public static void Main()
{
    int []arr = { 1, 5, 4, 2, 3 };
    int K = 2;
    int N = arr.Length;
    countOfSubsets(arr, N, K);
}
}
 
// This code is contributed by ipg2016107.


Javascript


输出:
4

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