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++
#include <bits/stdc++.h>
using namespace std;
int maximumgroups(vector< int >& arr, int N, int k)
{
sort(arr.begin(), arr.end());
int ans = 0, count = 1;
for ( int i = N - 1; i >= 0; i--) {
if (arr[i] * count >= k) {
ans++;
count = 1;
}
else {
count++;
}
}
return ans;
}
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
import java.util.Arrays;
class GFG {
public static int maximumgroups( int [] arr, int N, int k) {
Arrays.sort(arr);
int ans = 0 , count = 1 ;
for ( int i = N - 1 ; i >= 0 ; i--) {
if (arr[i] * count >= k) {
ans++;
count = 1 ;
}
else {
count++;
}
}
return ans;
}
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);
}
}
|
Python3
def maximumgroups(arr, N, k):
arr.sort();
ans = 0
count = 1 ;
for i in range (N - 1 , - 1 , - 1 ):
if (arr[i] * count > = k):
ans + = 1
count = 1 ;
else :
count + = 1
return ans;
if __name__ = = "__main__" :
N = 5 ;
k = 10 ;
arr = [ 7 , 11 , 2 , 9 , 5 ];
res = maximumgroups(arr, N, k);
print (res );
|
C#
using System;
public class GFG {
public static int maximumgroups( int [] arr, int N, int k) {
Array.Sort(arr);
int ans = 0, count = 1;
for ( int i = N - 1; i >= 0; i--) {
if (arr[i] * count >= k) {
ans++;
count = 1;
}
else {
count++;
}
}
return ans;
}
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);
}
}
|
Javascript
<script>
function maximumgroups(arr, N, k) {
arr.sort( function (a, b) { return a - b })
let ans = 0, count = 1;
for (let i = N - 1; i >= 0; i--) {
if (arr[i] * count >= k) {
ans++;
count = 1;
}
else {
count++;
}
}
return ans;
}
let N = 5;
let k = 10;
let arr = [7, 11, 2, 9, 5];
let res = maximumgroups(arr, N, k);
document.write(res)
</script>
|
Time Complexity: O(N*(log(N)))
Auxiliary Space: O(1)