最大化要形成的群,使得群的大小与其最小元素的乘积至少为 K
给定一个数组,长度为N的arr[]和一个整数K。第i 个元素的值为arr[i] 。任务是找到最大组数,使得对于每个组,该组中元素数与最小元素的乘积至少为K。
Note: Every element should belong to exactly one group and some elements may not be part of any group.
例子:
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.
方法:给定的问题可以通过贪心方法来解决。为了最大化组的数量,对数组进行排序并开始创建组,首先取较大的元素,因为这将帮助我们最大化每个组的最小元素。因此,每组所需的元素数量将减少,我们将最大化组数。请按照以下步骤解决问题:
- 按升序对给定数组进行排序。
- 将变量ans和count分别初始化为0和1 , ans将存储可以创建的组的总数, count将存储当前组的大小。
- 使用变量i从[N-1 到 0]遍历给定数组并执行以下步骤:
- 如果arr[i] (当前组的最小元素)和 count(当前组的大小)的乘积大于等于k ,则将count (组的总数)增加1并将计数初始化为1。
- 否则,将当前组中的元素数量增加1 。
- 完成这些步骤后,打印ans的值作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to return the maximum number
// of groups that can be formed from given array
int maximumgroups(vector& 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 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
输出
2
时间复杂度: O(N*(log(N)))
辅助空间: O(1)