给定一个大小为N的数组arr[] ,两个正整数M和K ,任务是将数组划分为M 个等长的子集,使得所有这些子集中第K个最大元素的总和最大。如果无法将数组划分为M 个等长的子集,则打印-1 。
例子:
Input: arr[] = { 1, 2, 3, 4, 5, 6 }, M = 2, K = 2
Output: 8
Explanation:
Length of each subset = (N / M) = 3.
Possible subsets from the array are: { {1, 3, 4}, {2, 5, 6} }
Therefore, the sum of Kth largest element from each subset = 3 + 5 = 8
Input: arr[] = {11, 20, 2, 17}, M = 4, K = 1
Output: 50
方法:该问题可以使用贪心技术解决。请按照以下步骤解决问题:
- 如果N不可整除M ,则打印-1 。
- 初始化一个变量,比如maxSum ,通过将数组划分为M 个等长子集来存储数组所有子集的第K个最大元素的最大可能总和。
- 按降序对数组进行排序。
- 使用变量i迭代范围[1, M]并更新maxSum += arr[i * K – 1]。
- 最后,打印maxSum的值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the maximum sum of Kth
// largest element of M equal length partition
int maximumKthLargestsumPart(int arr[], int N,
int M, int K)
{
// Stores sum of K_th largest element
// of equal length partitions
int maxSum = 0;
// If N is not
// divisible by M
if (N % M)
return -1;
// Stores length of
// each partition
int sz = (N / M);
// If K is greater than
// length of partition
if (K > sz)
return -1;
// Sort array in
// descending porder
sort(arr, arr + N, greater());
// Traverse the array
for (int i = 1; i <= M;
i++) {
// Update maxSum
maxSum
+= arr[i * K - 1];
}
return maxSum;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int M = 2;
int K = 1;
int N = sizeof(arr) / sizeof(arr[0]);
cout << maximumKthLargestsumPart(arr, N, M, K);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
import java.util.Collections;
class GFG{
// Function to find the maximum sum of Kth
// largest element of M equal length partition
static int maximumKthLargestsumPart(int[] arr, int N,
int M, int K)
{
// Stores sum of K_th largest element
// of equal length partitions
int maxSum = 0;
// If N is not
// divisible by M
if (N % M != 0)
return -1;
// Stores length of
// each partition
int sz = (N / M);
// If K is greater than
// length of partition
if (K > sz)
return -1;
// Sort array in
// descending porder
Arrays.sort(arr);
int i, k, t;
for(i = 0; i < N / 2; i++)
{
t = arr[i];
arr[i] = arr[N - i - 1];
arr[N - i - 1] = t;
}
// Traverse the array
for(i = 1; i <= M; i++)
{
// Update maxSum
maxSum += arr[i * K - 1];
}
return maxSum;
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 4, 5, 6 };
int M = 2;
int K = 1;
int N = arr.length;
System.out.println(maximumKthLargestsumPart(
arr, N, M, K));
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program to implement
# the above approach
# Function to find the maximum sum of Kth
# largest element of M equal length partition
def maximumKthLargestsumPart(arr, N, M, K):
# Stores sum of K_th largest element
# of equal length partitions
maxSum = 0
# If N is not
# divisible by M
if (N % M != 0):
return -1
# Stores length of
# each partition
sz = (N / M)
# If K is greater than
# length of partition
if (K > sz):
return -1
# Sort array in
# descending porder
arr = sorted(arr)
for i in range(0, N // 2):
t = arr[i]
arr[i] = arr[N - i - 1]
arr[N - i - 1] = t
# Traverse the array
for i in range(1, M + 1):
# Update maxSum
maxSum += arr[i * K - 1]
return maxSum
# Driver code
if __name__ == '__main__':
arr = [ 1, 2, 3, 4, 5, 6 ]
M = 2
K = 1
N = len(arr)
print(maximumKthLargestsumPart(arr, N, M, K))
# This code is contributed by Amit Katiyar
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to find the maximum sum of Kth
// largest element of M equal length partition
static int maximumKthLargestsumPart(int[] arr, int N,
int M, int K)
{
// Stores sum of K_th largest element
// of equal length partitions
int maxSum = 0;
// If N is not
// divisible by M
if (N % M != 0)
return -1;
// Stores length of
// each partition
int sz = (N / M);
// If K is greater than
// length of partition
if (K > sz)
return -1;
// Sort array in
// descending porder
Array.Sort(arr);
Array.Reverse(arr);
// Traverse the array
for(int i = 1; i <= M; i++)
{
// Update maxSum
maxSum += arr[i * K - 1];
}
return maxSum;
}
// Driver code
static void Main()
{
int[] arr = { 1, 2, 3, 4, 5, 6 };
int M = 2;
int K = 1;
int N = arr.Length;
Console.WriteLine(maximumKthLargestsumPart(
arr, N, M, K));
}
}
// This code is contributed by divyeshrabadiya07
Javascript
输出:
11
时间复杂度: O(N * log(N))
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live