Split array into maximum possible subsets having product of their length with the maximum element at least K
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: 3
Explanation:
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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the maximum // number of subsets into which // the array can be split void countOfSubsets( int arr[], int N, int K) { // Stores the maximum number of // subsets int count = 0; int pre = 1; // Sort the given array sort(arr, arr + N); // Iterate over the range [0, N] for ( int i = 0; i < N; i++) { // If current subset satisfies // the given condition if (arr[i] * pre >= K) { count++; pre = 1; } // Otherwise, increment pre else { pre++; } } cout << count; } // Driver Code 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
/*package whatever //do not write package name here */ import java.util.*; class GFG { // Function to find the maximum // number of subsets into which // the array can be split public static void countOfSubsets( int [] arr, int N, int K) { // Stores the maximum number of // subsets int count = 0 ; int pre = 1 ; // Sort the given array Arrays.sort(arr); // Iterate over the range [0, N] for ( int i = 0 ; i < N; i++) { // If current subset satisfies // the given condition if (arr[i] * pre >= K) { count++; pre = 1 ; } // Otherwise, increment pre 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
# Function to find the maximum # number of subsets into which # the array can be split def countOfSubsets(arr, N, K): # Stores the maximum number of # subsets count = 0 pre = 1 # Sort the given array arr.sort() # Iterate over the range [0, N] for i in range (N): # If current subset satisfies # the given condition if arr[i] * pre > = K: count = count + 1 pre = 1 # Otherwise, increment pre else : pre = pre + 1 print (count) # Driver code arr = [ 1 , 5 , 4 , 2 , 3 ] N = 5 K = 2 countOfSubsets(arr, N, K) # This code is contributed by maddler. |
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG{ // Function to find the maximum // number of subsets into which // the array can be split static void countOfSubsets( int []arr, int N, int K) { // Stores the maximum number of // subsets int count = 0; int pre = 1; // Sort the given array Array.Sort(arr); // Iterate over the range [0, N] for ( int i = 0; i < N; i++) { // If current subset satisfies // the given condition if (arr[i] * pre >= K) { count++; pre = 1; } // Otherwise, increment pre else { pre++; } } Console.Write(count); } // Driver Code public static void Main() { int []arr = { 1, 5, 4, 2, 3 }; int K = 2; int N = arr.Length; countOfSubsets(arr, N, K); } } // This code is contributed by ipg2016107. |
Javascript
<script> // JavaScript program for the above approach; // Function to find the maximum // number of subsets into which // the array can be split function countOfSubsets(arr, N, K) { // Stores the maximum number of // subsets let count = 0; let pre = 1; // Sort the given array arr.sort( function (a, b) { return a - b }); // Iterate over the range [0, N] for (let i = 0; i < N; i++) { // If current subset satisfies // the given condition if (arr[i] * pre >= K) { count++; pre = 1; } // Otherwise, increment pre else { pre++; } } document.write(count); } // Driver Code let arr = [1, 5, 4, 2, 3]; let K = 2; let N = arr.length; countOfSubsets(arr, N, K); // This code is contributed by Potta Lokesh </script> |
4
Time Complexity: O(N*log N)
Auxiliary Space: O(1)