Maximum number of subsets an array can be split into such that product of their minimums with size of subsets is at least K
Last Updated :
01 May, 2023
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 = 10
Output: 2
Explanation:
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 = 12
Output: 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.
- 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++
#include <bits/stdc++.h>
using namespace std;
int maximumSubset( int arr[], int N,
int K)
{
sort(arr, arr + N, greater< int >());
int len = 0;
int ans = 0;
for ( int i = 0; i < N; i++) {
len++;
if (arr[i] * len >= K) {
ans++;
len = 0;
}
}
return ans;
}
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
{
public static void reverse( int [] arr)
{
int n = arr.length;
for ( int i = 0 ; i < n / 2 ; i++) {
int temp = arr[i];
arr[i] = arr[n - i - 1 ];
arr[n - i - 1 ] = temp;
}
}
public static int maximumSubset( int arr[], int N, int K)
{
Arrays.sort(arr);
reverse(arr);
int len = 0 ;
int ans = 0 ;
for ( int i = 0 ; i < N; i++) {
len++;
if (arr[i] * len >= K) {
ans++;
len = 0 ;
}
}
return ans;
}
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));
}
}
|
Python3
def maximumSubset(arr, N,
K):
arr.sort(reverse = True )
len = 0
ans = 0
for i in range (N):
len + = 1
if (arr[i] * len > = K):
ans + = 1
len = 0
return ans
if __name__ = = "__main__" :
arr = [ 7 , 11 , 2 , 9 , 5 ]
K = 10
N = len (arr)
print (maximumSubset(arr, N, K))
|
C#
using System;
public class GFG
{
public static void reverse( int [] arr)
{
int n = arr.Length;
for ( int i = 0; i < n / 2; i++) {
int temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}
}
public static int maximumSubset( int []arr, int N, int K)
{
Array.Sort(arr);
reverse(arr);
int len = 0;
int ans = 0;
for ( int i = 0; i < N; i++)
{
len++;
if (arr[i] * len >= K)
{
ans++;
len = 0;
}
}
return ans;
}
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));
}
}
|
Javascript
<script>
function reverse(arr)
{
let n = arr.length;
for (let i = 0; i < n / 2; i++) {
let temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}
}
function maximumSubset(arr, N, K)
{
arr.sort();
arr.reverse();
let len = 0;
let ans = 0;
for (let i = 0; i < N; i++) {
len++;
if (arr[i] * len >= K) {
ans++;
len = 0;
}
}
return ans;
}
let arr = [ 7, 11, 2, 9, 5 ];
let K = 10;
let N =arr.length;
document.write(maximumSubset(arr, N, K));
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...