# Maximum number of subsets an array can be split into such that product of their minimums with size of subsets is at least K

Given an array** arr[]** consisting of **N** integers and an integer **K**, the task is to find the maximum number of disjoint subsets that the given array can be split into such that the product of the minimum element of each subset with the size of the subset is at least **K**.

**Examples:**

Input:arr[] = {7, 11, 2, 9, 5}, K = 10Output:2Explanation:

All such disjoint subsets possible are:Subset {11}:Product of minimum and size of the subset = 11 * 1 = 11 ( > 10).Subset {5, 9, 7}:Product of minimum and size of the subset = 5 * 3 = 15( > 10).

Therefore, the total number of subsets formed is 2.

Input:arr[] = {1, 3, 3, 7}, K = 12Output:0

**Approach:** The given problem can be solved greedily based on the following observations:

- As given in the problem statement the product of the minimum element of the formed subset and the length of the subset must be
**at least K**, so to maximize the number of subsets, the maximum element of the array can be grouped to the minimum element of the subset. - So the idea is to maximize the minimum element of the subset one by one, which maximizes the count of the subset.

Follow the steps below to solve the problem:

- Initialize a variable, say
**count**as**0**, to store the maximum number of subsets formed. - Initialize a variable, say
**length**as**0**, to store the length of the subset. - Sort the array in descending order.
- Traverse the given array
**arr[]**and perform the following steps:- Increment the value of
**length**by**1**. - If the value of
**(arr[i] * length)**is greater than**K**, then increment the value of the**count**by**1**and update the value of**length**as**0**.

- Increment the value of
- After completing the above steps, print the value of
**count**as the resultant maximum number of subsets formed.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the maximum number` `// of subsets possible such that` `// product of their minimums and the` `// size of subsets are at least K` `int` `maximumSubset(` `int` `arr[], ` `int` `N,` ` ` `int` `K)` `{` ` ` `// Sort the array in` ` ` `// descending order` ` ` `sort(arr, arr + N, greater<` `int` `>());` ` ` `// Stores the size of` ` ` `// the current subset` ` ` `int` `len = 0;` ` ` `// Stores the count of subsets` ` ` `int` `ans = 0;` ` ` `// Traverse the array arr[]` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `// Increment length of the` ` ` `// subsets by 1` ` ` `len++;` ` ` `// If arr[i] * len >= K` ` ` `if` `(arr[i] * len >= K) {` ` ` `// Increment ans by one` ` ` `ans++;` ` ` `// Update len` ` ` `len = 0;` ` ` `}` ` ` `}` ` ` `// Return the maximum possible` ` ` `// subsets formed` ` ` `return` `ans;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `arr[] = { 7, 11, 2, 9, 5 };` ` ` `int` `K = 10;` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `cout << maximumSubset(arr, N, K);` ` ` `return` `0;` `}` |

## Java

`import` `java.util.*;` `public` `class` `GFG` `{` ` ` `// Function to reverse the sorted array` ` ` `public` `static` `void` `reverse(` `int` `[] arr)` ` ` `{` ` ` `// Length of the array` ` ` `int` `n = arr.length;` ` ` `// Swaping the first half elements with last half` ` ` `// elements` ` ` `for` `(` `int` `i = ` `0` `; i < n / ` `2` `; i++) {` ` ` `// Storing the first half elements temporarily` ` ` `int` `temp = arr[i];` ` ` `// Assigning the first half to the last half` ` ` `arr[i] = arr[n - i - ` `1` `];` ` ` `// Assigning the last half to the first half` ` ` `arr[n - i - ` `1` `] = temp;` ` ` `}` ` ` `}` ` ` `// Function to find the maximum number` ` ` `// of subsets possible such that` ` ` `// product of their minimums and the` ` ` `// size of subsets are at least K` ` ` `public` `static` `int` `maximumSubset(` `int` `arr[], ` `int` `N, ` `int` `K)` ` ` `{` ` ` ` ` `// Sort the array in` ` ` `// descending order` ` ` `Arrays.sort(arr);` ` ` `reverse(arr);` ` ` `// Stores the size of` ` ` `// the current subset` ` ` `int` `len = ` `0` `;` ` ` `// Stores the count of subsets` ` ` `int` `ans = ` `0` `;` ` ` `// Traverse the array arr[]` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) {` ` ` `// Increment length of the` ` ` `// subsets by 1` ` ` `len++;` ` ` `// If arr[i] * len >= K` ` ` `if` `(arr[i] * len >= K) {` ` ` `// Increment ans by one` ` ` `ans++;` ` ` `// Update len` ` ` `len = ` `0` `;` ` ` `}` ` ` `}` ` ` `// Return the maximum possible` ` ` `// subsets formed` ` ` `return` `ans;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String args[]) {` ` ` `int` `arr[] = { ` `7` `, ` `11` `, ` `2` `, ` `9` `, ` `5` `};` ` ` `int` `K = ` `10` `;` ` ` `int` `N =arr.length;` ` ` `System.out.println(maximumSubset(arr, N, K));` ` ` `}` `}` `// This code is contributed by SoumikMondal` |

## Python3

`# Python 3 program for the above approach` `# Function to find the maximum number` `# of subsets possible such that` `# product of their minimums and the` `# size of subsets are at least K` `def` `maximumSubset(arr, N,` ` ` `K):` ` ` `# Sort the array in` ` ` `# descending order` ` ` `arr.sort(reverse ` `=` `True` `)` ` ` `# Stores the size of` ` ` `# the current subset` ` ` `len` `=` `0` ` ` `# Stores the count of subsets` ` ` `ans ` `=` `0` ` ` `# Traverse the array arr[]` ` ` `for` `i ` `in` `range` `(N):` ` ` `# Increment length of the` ` ` `# subsets by 1` ` ` `len` `+` `=` `1` ` ` `# If arr[i] * len >= K` ` ` `if` `(arr[i] ` `*` `len` `>` `=` `K):` ` ` `# Increment ans by one` ` ` `ans ` `+` `=` `1` ` ` `# Update len` ` ` `len` `=` `0` ` ` `# Return the maximum possible` ` ` `# subsets formed` ` ` `return` `ans` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `arr ` `=` `[` `7` `, ` `11` `, ` `2` `, ` `9` `, ` `5` `]` ` ` `K ` `=` `10` ` ` `N ` `=` `len` `(arr)` ` ` `print` `(maximumSubset(arr, N, K))` ` ` `# This code is contributed by ukasp.` |

## C#

`using` `System;` `public` `class` `GFG` `{` ` ` `// Function to reverse the sorted array` ` ` `public` `static` `void` `reverse(` `int` `[] arr)` ` ` `{` ` ` `// Length of the array` ` ` `int` `n = arr.Length;` ` ` `// Swaping the first half elements with last half` ` ` `// elements` ` ` `for` `(` `int` `i = 0; i < n / 2; i++) {` ` ` `// Storing the first half elements temporarily` ` ` `int` `temp = arr[i];` ` ` `// Assigning the first half to the last half` ` ` `arr[i] = arr[n - i - 1];` ` ` `// Assigning the last half to the first half` ` ` `arr[n - i - 1] = temp;` ` ` `}` ` ` `}` ` ` `// Function to find the maximum number` ` ` `// of subsets possible such that` ` ` `// product of their minimums and the` ` ` `// size of subsets are at least K` ` ` `public` `static` `int` `maximumSubset(` `int` `[]arr, ` `int` `N, ` `int` `K)` ` ` `{` ` ` `// Sort the array in` ` ` `// descending order` ` ` `Array.Sort(arr);` ` ` `reverse(arr);` ` ` ` ` `// Stores the size of` ` ` `// the current subset` ` ` `int` `len = 0;` ` ` `// Stores the count of subsets` ` ` `int` `ans = 0;` ` ` `// Traverse the array []arr` ` ` `for` `(` `int` `i = 0; i < N; i++)` ` ` `{` ` ` `// Increment length of the` ` ` `// subsets by 1` ` ` `len++;` ` ` `// If arr[i] * len >= K` ` ` `if` `(arr[i] * len >= K)` ` ` `{` ` ` `// Increment ans by one` ` ` `ans++;` ` ` `// Update len` ` ` `len = 0;` ` ` `}` ` ` `}` ` ` `// Return the maximum possible` ` ` `// subsets formed` ` ` `return` `ans;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `Main(String []args)` ` ` `{` ` ` `int` `[]arr = { 7, 11, 2, 9, 5 };` ` ` `int` `K = 10;` ` ` `int` `N = arr.Length;` ` ` `Console.WriteLine(maximumSubset(arr, N, K));` ` ` `}` `}` `// This code is contributed by aashish1995.` |

## Javascript

`<script>` `// JavaScript program to implement` `// the above approach` ` ` `// Function to reverse the sorted array` ` ` `function` `reverse(arr)` ` ` `{` ` ` ` ` `// Length of the array` ` ` `let n = arr.length;` ` ` ` ` `// Swaping the first half elements with last half` ` ` `// elements` ` ` `for` `(let i = 0; i < n / 2; i++) {` ` ` ` ` `// Storing the first half elements temporarily` ` ` `let temp = arr[i];` ` ` ` ` `// Assigning the first half to the last half` ` ` `arr[i] = arr[n - i - 1];` ` ` ` ` `// Assigning the last half to the first half` ` ` `arr[n - i - 1] = temp;` ` ` `}` ` ` `}` ` ` ` ` `// Function to find the maximum number` ` ` `// of subsets possible such that` ` ` `// product of their minimums and the` ` ` `// size of subsets are at least K` ` ` `function` `maximumSubset(arr, N, K)` ` ` `{` ` ` ` ` `// Sort the array in` ` ` `// descending order` ` ` `arr.sort();` ` ` `arr.reverse();` ` ` `// Stores the size of` ` ` `// the current subset` ` ` `let len = 0;` ` ` ` ` `// Stores the count of subsets` ` ` `let ans = 0;` ` ` ` ` `// Traverse the array arr[]` ` ` `for` `(let i = 0; i < N; i++) {` ` ` ` ` `// Increment length of the` ` ` `// subsets by 1` ` ` `len++;` ` ` ` ` `// If arr[i] * len >= K` ` ` `if` `(arr[i] * len >= K) {` ` ` ` ` `// Increment ans by one` ` ` `ans++;` ` ` ` ` `// Update len` ` ` `len = 0;` ` ` `}` ` ` `}` ` ` ` ` `// Return the maximum possible` ` ` `// subsets formed` ` ` `return` `ans;` ` ` `}` `// Driver code` ` ` `let arr = [ 7, 11, 2, 9, 5 ];` ` ` `let K = 10;` ` ` `let N =arr.length;` ` ` `document.write(maximumSubset(arr, N, K));` ` ` `</script>` |

**Output:**

2

**Time 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**.