📌  相关文章
📜  最大化可以将给定数组拆分成的子集计数,使其满足给定条件

📅  最后修改于: 2021-09-07 02:14:02             🧑  作者: Mango

给定一个大小为N的数组arr[]和一个正整数X ,任务是将数组划分为最大数量的子集,使得每个子集的最小元素与子集中元素数的乘积大于或等于K 。打印可能的此类子集的最大计数。

例子:

方法:该问题可以使用贪心技术解决。请按照以下步骤解决问题:

  • 按降序对数组元素进行排序。
  • 遍历数组并跟踪当前子集的大小
  • 由于数组按降序排序,子集最右边的元素将是当前划分的最小元素。
  • 因此,如果(当前子集的大小 * 当前元素)大于或等于X ,则增加计数并将当前分区的大小重置为0
  • 最后,打印获得的计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count maximum subsets into
// which the given array can be split such
// that it satisfies the given condition
void maxDivisions(int arr[], int N, int X)
{
 
    // Sort the array in decreasing order
    sort(arr, arr + N, greater());
 
    // Stores count of subsets possible
    int maxSub = 0;
 
    // Stores count of elements
    // in current subset
    int size = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        // Update size
        size++;
 
        // If product of the smallest element
        // present in the current subset and
        // size of current subset is >= K
        if (arr[i] * size >= X) {
 
            // Update maxSub
            maxSub++;
 
            // Update size
            size = 0;
        }
    }
 
    cout << maxSub << endl;
}
 
// Driver Code
int main()
{
 
    // Given array
    int arr[] = { 1, 3, 3, 7 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Given value of X
    int X = 3;
 
    maxDivisions(arr, N, X);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to count maximum subsets into
// which the given array can be split such
// that it satisfies the given condition
static void maxDivisions(Integer arr[], int N, int X)
{
 
    // Sort the array in decreasing order
    Arrays.sort(arr,Collections.reverseOrder());
 
    // Stores count of subsets possible
    int maxSub = 0;
 
    // Stores count of elements
    // in current subset
    int size = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++)
    {
 
        // Update size
        size++;
 
        // If product of the smallest element
        // present in the current subset and
        // size of current subset is >= K
        if (arr[i] * size >= X)
        {
 
            // Update maxSub
            maxSub++;
 
            // Update size
            size = 0;
        }
    }
    System.out.print(maxSub +"\n");
}
 
// Driver Code
public static void main(String[] args)
{
 
    // Given array
    Integer arr[] = { 1, 3, 3, 7 };
 
    // Size of the array
    int N = arr.length;
 
    // Given value of X
    int X = 3;
    maxDivisions(arr, N, X);
 
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
 
# Function to count maximum subsets into
# which the given array can be split such
# that it satisfies the given condition
def maxDivisions(arr, N, X) :
 
    # Sort the array in decreasing order
    arr.sort(reverse = True)
     
    # Stores count of subsets possible
    maxSub = 0;
 
    # Stores count of elements
    # in current subset
    size = 0;
 
    # Traverse the array arr[]
    for i in range(N) :
 
        # Update size
        size += 1;
 
        # If product of the smallest element
        # present in the current subset and
        # size of current subset is >= K
        if (arr[i] * size >= X) :
 
            # Update maxSub
            maxSub += 1;
 
            # Update size
            size = 0;
    print(maxSub);
 
# Driver Code
if __name__ == "__main__" :
 
    # Given array
    arr = [ 1, 3, 3, 7 ];
 
    # Size of the array
    N = len(arr);
 
    # Given value of X
    X = 3;
 
    maxDivisions(arr, N, X);
     
    # This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to count maximum subsets into
  // which the given array can be split such
  // that it satisfies the given condition
  static void maxDivisions(int[] arr, int N, int X)
  {
 
    // Sort the array in decreasing order
    Array.Sort(arr);
    Array.Reverse(arr);
 
    // Stores count of subsets possible
    int maxSub = 0;
 
    // Stores count of elements
    // in current subset
    int size = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++)
    {
 
      // Update size
      size++;
 
      // If product of the smallest element
      // present in the current subset and
      // size of current subset is >= K
      if (arr[i] * size >= X)
      {
 
        // Update maxSub
        maxSub++;
 
        // Update size
        size = 0;
      }
    }
 
    Console.WriteLine(maxSub);
  }
 
  // Driver Code
  public static void Main()
  {
 
    // Given array
    int[] arr = { 1, 3, 3, 7 };
 
    // Size of the array
    int N = arr.Length;
 
    // Given value of X
    int X = 3;
    maxDivisions(arr, N, X);
  }
}
 
// This code is contributed by subhammahato348.


Javascript


输出:
3

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

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