# Maximize count of subsets into which the given array can be split such that it satisfies the given condition

• Last Updated : 08 Sep, 2021

Given an array arr[] of size N and a positive integer X, the task is to partition the array into the maximum number of subsets such that the multiplication of the smallest element of each subset with the count of elements in the subsets is greater than or equal to K. Print the maximum count of such subsets possible.

Examples:

Input: arr[] = {1, 3, 3, 7}, X = 3
Output: 3
Explanation: Partition the array into 3 subsets { {1, 3}, {3}, {7} }. Therefore, the required output is 3.

Input: arr[] = {2, 4, 2, 5, 1}, X = 2
Output: 4

Approach: The problem can be solved using the Greedy technique. Follow the steps below to solve the problem:

• Sort the array elements in decreasing order.
• Traverse the array and keep track of the size of the current subset
• As the array is sorted in decreasing order, the rightmost element of the subset will be the smallest element of the current division.
• So, if (size of current subset * current element) is greater than or equal to X, then increment the count and reset the size of the current partition to 0.
• Finally, print the count obtained.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to count maximum subsets into``// which the given array can be split such``// that it satisfies the given condition``void` `maxDivisions(``int` `arr[], ``int` `N, ``int` `X)``{` `    ``// Sort the array in decreasing order``    ``sort(arr, arr + N, greater<``int``>());` `    ``// Stores count of subsets possible``    ``int` `maxSub = 0;` `    ``// Stores count of elements``    ``// in current subset``    ``int` `size = 0;` `    ``// Traverse the array arr[]``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// Update size``        ``size++;` `        ``// If product of the smallest element``        ``// present in the current subset and``        ``// size of current subset is >= K``        ``if` `(arr[i] * size >= X) {` `            ``// Update maxSub``            ``maxSub++;` `            ``// Update size``            ``size = 0;``        ``}``    ``}` `    ``cout << maxSub << endl;``}` `// Driver Code``int` `main()``{` `    ``// Given array``    ``int` `arr[] = { 1, 3, 3, 7 };` `    ``// Size of the array``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);` `    ``// Given value of X``    ``int` `X = 3;` `    ``maxDivisions(arr, N, X);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;``class` `GFG``{` `// Function to count maximum subsets into``// which the given array can be split such``// that it satisfies the given condition``static` `void` `maxDivisions(Integer arr[], ``int` `N, ``int` `X)``{` `    ``// Sort the array in decreasing order``    ``Arrays.sort(arr,Collections.reverseOrder());` `    ``// Stores count of subsets possible``    ``int` `maxSub = ``0``;` `    ``// Stores count of elements``    ``// in current subset``    ``int` `size = ``0``;` `    ``// Traverse the array arr[]``    ``for` `(``int` `i = ``0``; i < N; i++)``    ``{` `        ``// Update size``        ``size++;` `        ``// If product of the smallest element``        ``// present in the current subset and``        ``// size of current subset is >= K``        ``if` `(arr[i] * size >= X)``        ``{` `            ``// Update maxSub``            ``maxSub++;` `            ``// Update size``            ``size = ``0``;``        ``}``    ``}``    ``System.out.print(maxSub +``"\n"``);``}` `// Driver Code``public` `static` `void` `main(String[] args)``{` `    ``// Given array``    ``Integer arr[] = { ``1``, ``3``, ``3``, ``7` `};` `    ``// Size of the array``    ``int` `N = arr.length;` `    ``// Given value of X``    ``int` `X = ``3``;``    ``maxDivisions(arr, N, X);` `}``}` `// This code is contributed by shikhasingrajput`

## Python3

 `# Python3 program for the above approach` `# Function to count maximum subsets into``# which the given array can be split such``# that it satisfies the given condition``def` `maxDivisions(arr, N, X) :` `    ``# Sort the array in decreasing order``    ``arr.sort(reverse ``=` `True``)``    ` `    ``# Stores count of subsets possible``    ``maxSub ``=` `0``;` `    ``# Stores count of elements``    ``# in current subset``    ``size ``=` `0``;` `    ``# Traverse the array arr[]``    ``for` `i ``in` `range``(N) :` `        ``# Update size``        ``size ``+``=` `1``;` `        ``# If product of the smallest element``        ``# present in the current subset and``        ``# size of current subset is >= K``        ``if` `(arr[i] ``*` `size >``=` `X) :` `            ``# Update maxSub``            ``maxSub ``+``=` `1``;` `            ``# Update size``            ``size ``=` `0``;``    ``print``(maxSub);` `# Driver Code``if` `__name__ ``=``=` `"__main__"` `:` `    ``# Given array``    ``arr ``=` `[ ``1``, ``3``, ``3``, ``7` `];` `    ``# Size of the array``    ``N ``=` `len``(arr);` `    ``# Given value of X``    ``X ``=` `3``;` `    ``maxDivisions(arr, N, X);``    ` `    ``# This code is contributed by AnkThon`

## C#

 `// C# program for the above approach``using` `System;``class` `GFG``{` `  ``// Function to count maximum subsets into``  ``// which the given array can be split such``  ``// that it satisfies the given condition``  ``static` `void` `maxDivisions(``int``[] arr, ``int` `N, ``int` `X)``  ``{` `    ``// Sort the array in decreasing order``    ``Array.Sort(arr);``    ``Array.Reverse(arr);` `    ``// Stores count of subsets possible``    ``int` `maxSub = 0;` `    ``// Stores count of elements``    ``// in current subset``    ``int` `size = 0;` `    ``// Traverse the array arr[]``    ``for` `(``int` `i = 0; i < N; i++)``    ``{` `      ``// Update size``      ``size++;` `      ``// If product of the smallest element``      ``// present in the current subset and``      ``// size of current subset is >= K``      ``if` `(arr[i] * size >= X)``      ``{` `        ``// Update maxSub``        ``maxSub++;` `        ``// Update size``        ``size = 0;``      ``}``    ``}` `    ``Console.WriteLine(maxSub);``  ``}` `  ``// Driver Code``  ``public` `static` `void` `Main()``  ``{` `    ``// Given array``    ``int``[] arr = { 1, 3, 3, 7 };` `    ``// Size of the array``    ``int` `N = arr.Length;` `    ``// Given value of X``    ``int` `X = 3;``    ``maxDivisions(arr, N, X);``  ``}``}` `// This code is contributed by subhammahato348.`

## Javascript

 ``

Output:

`3`

Time Complexity: O(N * log(N))
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up