Maximize groups to be formed such that product of size of group with its minimum element is at least K
Last Updated :
07 Jan, 2022
Given an array, arr[] of length N, and an integer K. The value of the i-th element is arr[i]. The task is to find the maximum number of groups such that for each group the product of the number of elements in that group and the minimum element is at least K.
Note: Every element should belong to exactly one group and some elements may not be part of any group.
Examples:
Input: N=5, arr[]={7, 2, 11, 9, 5}, K=10
Output: 2
Explanation:
- Make one group [11, 7] (group size=2) where product of size of the group and minimum element of group is 2*7=14 which is greater than k
- Make another group [9, 5] (group size=2) where product of size of group and minimum element of group is 2*5=10 is equal to k.
- Thus we can make maximum 2 groups
Input: N=4, arr[]={1, 7, 3, 3}, K=11
Output: 0
Explanation:
- If we make a group [7, 3, 3] then product of size of group (3) and minimum element of the group(3) is 3*3=9 which is less than k.
- If we make a group [7, 3, 3, 1] then product of size of group (4) and minimum element of the group(1) is 1*4=4 which is less than k.
- If we make a group [7, 3] then product of size of group (2) and minimum element of the group(3) is 2*3=6 which is less than k.
- Thus we cannot make any group with the given array such that the product of size of group and the minimum element is at least k.
Approach: The given problem can be solved by a greedy approach. To maximize the number of groups, sort the array and start creating the groups by taking the bigger elements first because this will help us in maximizing the minimum element of each group. Thus the number of elements required in each group will reduce and we will maximize the number of groups. Follow the steps below to solve the problem:
- Sort the given array in increasing order.
- Initialize variables ans and count to 0 and 1 respectively, ans will store the total number of groups that can be created and count will store the size of the current group.
- Traverse the given array from [N-1 to 0] using the variable i and perform these steps :
- If the product of arr[i] (minimum element of the current group ) and count (size of the current group) is greater equal to k then increase the count (total number of groups) by 1 and initialize the count to 1.
- Otherwise, increase the number of elements in the current group by 1.
- After completing these steps print the value of ans as the answer.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the maximum number
// of groups that can be formed from given array
int maximumgroups(vector<int>& arr, int N, int k)
{
// Sorting the given array in increasing order
sort(arr.begin(), arr.end());
int ans = 0, count = 1;
// Start creating the groups by taking
// the bigger elements first because this
// will help us in maximizing our
// minimum element of each group
for (int i = N - 1; i >= 0; i--) {
// If the product of minimum element of
// current group and count is greater equal
// to k then increase the ans by one and
// initialize the count to 1
if (arr[i] * count >= k) {
ans++;
count = 1;
}
// Otherwise increase the number of elements
// in the current group by one
else {
count++;
}
}
// Return the maximum number of groups possible
return ans;
}
// Driver Code
int main()
{
int N = 5;
int k = 10;
vector<int> arr = { 7, 11, 2, 9, 5 };
int res = maximumgroups(arr, N, k);
cout << res << endl;
return 0;
}
Java
// Java program for the above approach
import java.util.Arrays;
class GFG {
// Function to return the maximum number
// of groups that can be formed from given array
public static int maximumgroups(int[] arr, int N, int k) {
// Sorting the given array in increasing order
Arrays.sort(arr);
int ans = 0, count = 1;
// Start creating the groups by taking
// the bigger elements first because this
// will help us in maximizing our
// minimum element of each group
for (int i = N - 1; i >= 0; i--) {
// If the product of minimum element of
// current group and count is greater equal
// to k then increase the ans by one and
// initialize the count to 1
if (arr[i] * count >= k) {
ans++;
count = 1;
}
// Otherwise increase the number of elements
// in the current group by one
else {
count++;
}
}
// Return the maximum number of groups possible
return ans;
}
// Driver Code
public static void main(String args[]) {
int N = 5;
int k = 10;
int[] arr = { 7, 11, 2, 9, 5 };
int res = maximumgroups(arr, N, k);
System.out.println(res);
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# Python 3 program for the above approach
# Function to return the maximum number
# of groups that can be formed from given array
def maximumgroups(arr, N, k):
# Sorting the given array in increasing order
arr.sort();
ans = 0
count = 1;
# Start creating the groups by taking
# the bigger elements first because this
# will help us in maximizing our
# minimum element of each group
for i in range(N - 1, -1, -1):
# If the product of minimum element of
# current group and count is greater equal
# to k then increase the ans by one and
# initialize the count to 1
if (arr[i] * count >= k):
ans += 1
count = 1;
# Otherwise increase the number of elements
# in the current group by one
else:
count += 1
# Return the maximum number of groups possible
return ans;
# Driver Code
if __name__ == "__main__":
N = 5;
k = 10;
arr = [ 7, 11, 2, 9, 5 ];
res = maximumgroups(arr, N, k);
print(res );
# This code is contributed by ukasp.
C#
// C# program for the above approach
using System;
public class GFG {
// Function to return the maximum number
// of groups that can be formed from given array
public static int maximumgroups(int[] arr, int N, int k) {
// Sorting the given array in increasing order
Array.Sort(arr);
int ans = 0, count = 1;
// Start creating the groups by taking
// the bigger elements first because this
// will help us in maximizing our
// minimum element of each group
for (int i = N - 1; i >= 0; i--) {
// If the product of minimum element of
// current group and count is greater equal
// to k then increase the ans by one and
// initialize the count to 1
if (arr[i] * count >= k) {
ans++;
count = 1;
}
// Otherwise increase the number of elements
// in the current group by one
else {
count++;
}
}
// Return the maximum number of groups possible
return ans;
}
// Driver Code
public static void Main(String []args) {
int N = 5;
int k = 10;
int[] arr = { 7, 11, 2, 9, 5 };
int res = maximumgroups(arr, N, k);
Console.WriteLine(res);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// JavaScript code for the above approach
// Function to return the maximum number
// of groups that can be formed from given array
function maximumgroups(arr, N, k) {
// Sorting the given array in increasing order
arr.sort(function (a, b) { return a - b })
let ans = 0, count = 1;
// Start creating the groups by taking
// the bigger elements first because this
// will help us in maximizing our
// minimum element of each group
for (let i = N - 1; i >= 0; i--) {
// If the product of minimum element of
// current group and count is greater equal
// to k then increase the ans by one and
// initialize the count to 1
if (arr[i] * count >= k) {
ans++;
count = 1;
}
// Otherwise increase the number of elements
// in the current group by one
else {
count++;
}
}
// Return the maximum number of groups possible
return ans;
}
// Driver Code
let N = 5;
let k = 10;
let arr = [7, 11, 2, 9, 5];
let res = maximumgroups(arr, N, k);
document.write(res)
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N*(log(N)))
Auxiliary Space: O(1)
Similar Reads
Maximize number of groups formed with size not smaller than its largest element Given an array arr[] of N positive integers(1 ? arr[i] ? N ), divide the elements of the array into groups such that the size of each group is greater than or equal to the largest element of that group. It may be also possible that an element cannot join any group. The task is to maximize the number
7 min read
Maximize the sum of maximum elements of at least K-sized subarrays Given an integer array arr[] of length N and an integer K, partition the array in some non-overlapping subarrays such that each subarray has size at least K and each element of the array should be part of a subarray. The task is to maximize the sum of maximum elements across all the subarrays. Examp
7 min read
Maximum number of subsets an array can be split into such that product of their minimums with size of subsets is at least K Given an array arr[] consisting of N integers and an integer K, the task is to find the maximum number of disjoint subsets that the given array can be split into such that the product of the minimum element of each subset with the size of the subset is at least K. Examples: Input: arr[] = {7, 11, 2,
8 min read
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: 3Explan
5 min read
Minimum count of groups such that sum of adjacent elements is divisible by K in each group Given an array arr[] consisting of N integers, and an integer K, the task is to print the minimum count of groups formed by taking elements of the array such that the sum of adjacent elements is divisible by K in each group. Examples: Input: arr[] = {2, 6, 5, 8, 2, 9}, K = 2Output: 2The array can be
10 min read
Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k Given an array arr[] and an integer k, the task is to find the maximum product from the array such that the frequency sum of all repeating elements in the product is ⤠2 * k, where frequency sum is the sum of frequencies of all the elements in the product that appear more than once. For example, if
7 min read