Maximize count of subsets having product of smallest element and size of the subset at least X
Last Updated :
04 Sep, 2021
Given an array arr[] consisting of N integers and an integer X, the task is to count the maximum number of subsets possible from the given array having
Smallest element of the Subset * Size of the Subset ? X
Examples:
Input: arr[] = {7, 11, 2, 9, 5}, X = 10
Output: 2
Explanation:
One of the possible solution is {7, 9} and {11, 5}.
In subset {7, 9} the smallest element (= 7), size of subset (= 2).
Therefore, the product of the smallest element of the subset and the size of the subset is equal to 14 ( > 10).
In subset {11, 5} the smallest element (= 5), size of subset (= 2).
Therefore, the product of the smallest element of the subset * size of the subset is equal to 10
Hence, the required output is 2
Input: arr[] = {2, 4, 2, 3}, X = 8
Output: 1
Approach: The problem can be solved using Greedy Approach. The idea is to sort the array in descending order and then traverse the elements one by one to check the required conditions.
Follow the below steps to solve the problem.
- Sort the array in descending order.
- Initialize variables counter, sz to store the count of possible subsets, and the size of the current subset.
- Iterate over the given array and check if arr[i] * sz ? X or not. If found to be true, reset the sz and increment counter by 1.
- Finally, after complete traversal of the array, print counter as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool comp( int a, int b)
{
return a > b;
}
int maxSubset( int arr[], int N, int X)
{
sort(arr, arr + N, comp);
int counter = 0;
int sz = 0;
for ( int i = 0; i < N; i++)
{
sz++;
if (arr[i] * sz >= X) {
counter++;
sz = 0;
}
}
return counter;
}
int main()
{
int arr[] = { 7, 11, 2, 9, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
int X = 10;
cout << maxSubset(arr, N, X);
}
|
Java
import java.util.*;
class GFG{
static int maxSubset(Integer arr[], int N,
int X)
{
Arrays.sort(arr, Collections.reverseOrder());
int counter = 0 ;
int sz = 0 ;
for ( int i = 0 ; i < N; i++)
{
sz++;
if (arr[i] * sz >= X)
{
counter++;
sz = 0 ;
}
}
return counter;
}
public static void main(String[] args)
{
Integer arr[] = { 7 , 11 , 2 , 9 , 5 };
int N = arr.length;
int X = 10 ;
System.out.print(maxSubset(arr, N, X));
}
}
|
Python3
def maxSubset(arr, N, X):
arr.sort(reverse = True )
counter = 0
sz = 0
for i in range (N):
sz + = 1
if (arr[i] * sz > = X):
counter + = 1
sz = 0
return counter
arr = [ 7 , 11 , 2 , 9 , 5 ]
N = len (arr)
X = 10
print (maxSubset(arr, N, X))
|
C#
using System;
using System.Linq;
class GFG{
static int maxSubset( int []arr, int N,
int X)
{
Array.Sort(arr);
Array.Reverse(arr);
int counter = 0;
int sz = 0;
for ( int i = 0; i < N; i++)
{
sz++;
if (arr[i] * sz >= X)
{
counter++;
sz = 0;
}
}
return counter;
}
public static void Main(String[] args)
{
int []arr = { 7, 11, 2, 9, 5 };
int N = arr.Length;
int X = 10;
Console.Write(maxSubset(arr, N, X));
}
}
|
Javascript
<script>
function comp(a, b)
{
return a > b;
}
function maxSubset(arr, N, X)
{
arr = arr.sort((a, b)=>b-a);
let counter = 0;
let sz = 0;
for (let i = 0; i < N; i++)
{
sz++;
if (arr[i] * sz >= X) {
counter++;
sz = 0;
}
}
return counter;
}
let arr = [ 7, 11, 2, 9, 5 ];
let N = arr.length;
let X = 10;
document.write(maxSubset(arr, N, X));
</script>
|
Time Complexity: O(NLog(N))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...