# Split array into maximum possible subsets having product of their length with the maximum element at least K

Given an array arr[] consisting of N integers and a positive integer K, the task is to maximize the number of subsets having a product of its size and its maximum element at least K by dividing array element into disjoint subsets.

Examples:

Input: N = 5, K = 4, arr[] = {1, 5, 4, 2, 3}
Output: 3
Explanation:
The array can be split into 3 possible subsets {1, 2}, {4, 3} and {5}, whose product of maximum element of the subsets with its size is at least K(= 4). Therefore, the count of such subsets is 3.

Input: N = 4, K = 81, arr[] = {12, 8, 14, 20}
Output: 0

Approach: The given problem can be solved by observing the fact that the elements that are at least K can form a suitable group alone. In order to extract the maximum number of groups that don’t have elements greater than equal to K, the idea is to start including elements from the minimum element of the array arr[] and then keep moving to larger elements until a suitable group is formed. Follow the steps below to solve the problem:

• Initialize a variable, say count as 0, that stores the resultant count of subsets and pre as 1 to store the initial size of the group.
• Sort the given array arr[] in ascending order.
• Iterate over the range [0, N] using a variable, say i, and perform the following steps:
• If the value of arr[i] * pre is at least K, then increment the value of count by 1 and set the value of pre as 1.
• Otherwise, increment the value of pre by 1.
• After performing the above steps, print the value of count as the resultant count of subsets.

Below is the implementation of the above approach:

## 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);`   `    ``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

 ``

Output:

`4`

Time Complexity: O(N*log N)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next