📌  相关文章
📜  将数组拆分为等长的子集,每个子集的第K个最大元素的和为最大

📅  最后修改于: 2021-05-13 22:39:57             🧑  作者: Mango

给定大小为N的数组arr [] ,两个正整数MK ,任务是将数组划分为M个相等长度的子集,以使所有这些子集的第K最大元素的总和最大。如果无法将数组划分为M个相等长度的子集,则打印-1

例子:

方法:可以使用贪婪技术解决问题。请按照以下步骤解决问题:

  • 如果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)