Maximize count of subsets having product of smallest element and size of the subset at least X

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:



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. 

  1. Sort the array in descending order.
  2. Initialize variables counter, sz to store the count of possible subsets, and the size of the current subset.
  3. 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.
  4. Finally, after complete traversal of the array, print counter as the required answer.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to implement
// the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Comperator function to return
// the greater of two numbers
bool comp(int a, int b)
{
    return a > b;
}
  
// Function to return the maximum count
// of subsets possible which
// satisy the above condition
int maxSubset(int arr[], int N, int X)
{
  
    // Sort the array in
    // descending order
    sort(arr, arr + N, comp);
  
    // Stores the count of subsets
    int counter = 0;
  
    // Stores the size of
    // the current subset
    int sz = 0;
  
    for (int i = 0; i < N; i++) 
    {
        sz++;
  
        // Check for the necessary
        // conditions
        if (arr[i] * sz >= X) {
            counter++;
            sz = 0;
        }
    }
    return counter;
}
  
// Driver Code
int main()
{
    int arr[] = { 7, 11, 2, 9, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int X = 10;
    cout << maxSubset(arr, N, X);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
import java.util.*;
  
class GFG{
  
// Function to return the maximum count
// of subsets possible which
// satisy the above condition
static int maxSubset(Integer arr[], int N, 
                                    int X)
{
      
    // Sort the array in
    // descending order
    Arrays.sort(arr, Collections.reverseOrder());
  
    // Stores the count of subsets
    int counter = 0;
  
    // Stores the size of
    // the current subset
    int sz = 0;
  
    for(int i = 0; i < N; i++) 
    {
        sz++;
  
        // Check for the necessary
        // conditions
        if (arr[i] * sz >= X)
        {
            counter++;
            sz = 0;
        }
    }
    return counter;
}
  
// Driver Code
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));
}
}
  
// This code is contributed by 29AjayKumar 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to implement
# the above approach
  
# Function to return the maximum count
# of subsets possible which
# satisy the above condition
def maxSubset(arr, N, X):
  
    # Sort the array in
    # descending order
    arr.sort(reverse = True)
  
    # Stores the count of subsets
    counter = 0
  
    # Stores the size of
    # the current subset
    sz = 0
  
    for i in range(N):
        sz += 1
  
        # Check for the necessary
        # conditions 
        if(arr[i] * sz >= X):
            counter += 1
            sz = 0
  
    return counter
  
# Driver Code
  
# Given array
arr = [ 7, 11, 2, 9, 5 ]
N = len(arr)
X = 10
  
# Function call
print(maxSubset(arr, N, X))
  
# This code is contributed by Shivam Singh

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement
// the above approach
using System;
using System.Linq;
  
class GFG{
  
// Function to return the maximum count
// of subsets possible which
// satisy the above condition
static int maxSubset(int []arr, int N, 
                                int X)
{
      
    // Sort the array in
    // descending order
    Array.Sort(arr);
    Array.Reverse(arr);
  
    // Stores the count of subsets
    int counter = 0;
  
    // Stores the size of
    // the current subset
    int sz = 0;
  
    for(int i = 0; i < N; i++) 
    {
        sz++;
  
        // Check for the necessary
        // conditions
        if (arr[i] * sz >= X)
        {
            counter++;
            sz = 0;
        }
    }
    return counter;
}
  
// Driver Code
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));
}
}
  
// This code is contributed by shikhasingrajput

chevron_right


Output: 

2

Time Complexity: O(NLog(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.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.