- Arrays, Mathematical, partition, Sorting, subset

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: 3Explanation: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: 0Approach: 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++#include using namespace std;void countOfSubsets(int arr[], int N,                    int K){            int count = 0;    int pre = 1;        sort(arr, arr + N);        for (int i = 0; i < N; i++) {                        if (arr[i] * pre >= K) {            count++;            pre = 1;        }                else {            pre++;        }    }    cout = K) {                count++;                pre = 1;            }                        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);    }}Javascript                                        function countOfSubsets(arr, N,            K) {                                    let count = 0;            let pre = 1;                        arr.sort(function (a, b) { return a – b });                        for (let i = 0; i < N; i++) {                                                if (arr[i] * pre >= K) {                    count++;                    pre = 1;                }                                else {                    pre++;                }            }            document.write(count);        }                let arr = [1, 5, 4, 2, 3];        let K = 2;        let N = arr.length;        countOfSubsets(arr, N, K);       Output: 4Time Complexity: O(N*log N)Auxiliary Space: O(1)Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.