Split array into maximum possible subsets having product of their length with the maximum element at least K
Last Updated :
04 Aug, 2021
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: 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++
#include <bits/stdc++.h>
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 << count;
}
int main()
{
int arr[] = { 1, 5, 4, 2, 3 };
int K = 2;
int N = sizeof(arr) / sizeof(arr[0]);
countOfSubsets(arr, N, K);
return 0;
}
Java
import java.util.*;
class GFG {
public static void countOfSubsets(int[] arr, int N,
int K)
{
int count = 0;
int pre = 1;
Arrays.sort(arr);
for (int i = 0; i < N; i++) {
if (arr[i] * pre >= 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);
}
}
Python3
def countOfSubsets(arr, N, K):
count = 0
pre = 1
arr.sort()
for i in range(N):
if arr[i] * pre >= K:
count = count + 1
pre = 1
else:
pre = pre + 1
print(count)
arr = [1, 5, 4, 2, 3]
N = 5
K = 2
countOfSubsets(arr, N, K)
C#
using System;
using System.Collections.Generic;
class GFG{
static void countOfSubsets(int []arr, int N,
int K)
{
int count = 0;
int pre = 1;
Array.Sort(arr);
for (int i = 0; i < N; i++) {
if (arr[i] * pre >= K) {
count++;
pre = 1;
}
else {
pre++;
}
}
Console.Write(count);
}
public static void Main()
{
int []arr = { 1, 5, 4, 2, 3 };
int K = 2;
int N = arr.Length;
countOfSubsets(arr, N, K);
}
}
Javascript
<script>
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);
</script>
Time Complexity: O(N*log N)Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Add Your Comment
Please Login to comment...