给定大小为N的数组arr []和正整数X ,任务是将数组划分为最大数量的子集,以使每个子集的最小元素与子集中元素数的乘积大于或等于等于K。打印此类子集的最大数量。
例子:
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
方法:可以使用贪婪技术解决问题。请按照以下步骤解决问题:
- 以降序对数组元素进行排序。
- 遍历数组并跟踪当前子集的大小
- 当数组以降序排序时,子集中最右边的元素将是当前除法中最小的元素。
- 因此,如果(当前子集的大小*当前元素的大小)大于或等于X ,则递增计数并将当前分区的大小重置为0 。
- 最后,打印获得的计数。
下面是上述方法的实现:
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());
// 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.
输出:
3
时间复杂度: O(N * log(N))
辅助空间: O(1)