给定由N个整数和整数K组成的数组arr [] ,任务是将数组拆分为K个子集( N%K = 0 ),以使所有子集的第二大元素的总和最大化。
例子:
Input: arr[] = {1, 3, 1, 5, 1, 3}, K = 2
Output: 4
Explanation: Splitting the array into the subsets {1, 1, 3} and {1, 3, 5} maximizes the sum of second maximum elements in the two arrays.
Input: arr[] = {1, 2, 5, 8, 6, 4, 3, 4, 9}, K = 3
Output: 17
方法:想法是对数组进行排序,并在从数组中第二大元素开始,反向遍历数组时,不断添加遇到的第二个元素,恰好是K次。请按照以下步骤解决问题:
- 以升序对数组进行排序。
- 反向遍历数组arr [] 。
- 初始化i = N – 1 。
- 迭代K次,并将arr [i – 1]添加到总和中,并将i减少2 。
- 最后,打印获得的总和。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to split array into
// K subsets having maximum
// sum of their second maximum elements
void splitArray(int arr[], int n, int K)
{
// Sort the array
sort(arr, arr + n);
int i = n - 1;
// Stores the maximum possible
// sum of second maximums
int result = 0;
while (K--) {
// Add second maximum
// of current subset
result += arr[i - 1];
// Proceed to the second
// maximum of next subset
i -= 2;
}
// Print the maximum
// sum obtained
cout << result;
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 1, 3, 1, 5, 1, 3 };
// Size of array
int N = sizeof(arr)
/ sizeof(arr[0]);
int K = 2;
// Function Call
splitArray(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.Arrays;
class GFG
{
// Function to split array into
// K subsets having maximum
// sum of their second maximum elements
static void splitArray(int arr[], int n, int K)
{
// Sort the array
Arrays.sort(arr);
int i = n - 1;
// Stores the maximum possible
// sum of second maximums
int result = 0;
while (K-- != 0)
{
// Add second maximum
// of current subset
result += arr[i - 1];
// Proceed to the second
// maximum of next subset
i -= 2;
}
// Print the maximum
// sum obtained
System.out.print(result);
}
// Drive Code
public static void main(String[] args)
{
// Given array arr[]
int[] arr = { 1, 3, 1, 5, 1, 3 };
// Size of array
int N = arr.length;
int K = 2;
// Function Call
splitArray(arr, N, K);
}
}
// This code is contributed by sanjoy_62.
Python3
# Python3 program to implement
# the above approach
# Function to split array into K
# subsets having maximum sum of
# their second maximum elements
def splitArray(arr, n, K):
# Sort the array
arr.sort()
i = n - 1
# Stores the maximum possible
# sum of second maximums
result = 0
while (K > 0):
# Add second maximum
# of current subset
result += arr[i - 1]
# Proceed to the second
# maximum of next subset
i -= 2
K -= 1
# Print the maximum
# sum obtained
print(result)
# Driver Code
if __name__ == "__main__":
# Given array arr[]
arr = [ 1, 3, 1, 5, 1, 3 ]
# Size of array
N = len(arr)
K = 2
# Function Call
splitArray(arr, N, K)
# This code is contributed by chitranayal
C#
// C# program for the above approach
using System;
class GFG
{
// Function to split array into
// K subsets having maximum
// sum of their second maximum elements
static void splitArray(int []arr, int n, int K)
{
// Sort the array
Array.Sort(arr);
int i = n - 1;
// Stores the maximum possible
// sum of second maximums
int result = 0;
while (K-- != 0)
{
// Add second maximum
// of current subset
result += arr[i - 1];
// Proceed to the second
// maximum of next subset
i -= 2;
}
// Print the maximum
// sum obtained
Console.Write(result);
}
// Drive Code
public static void Main(String[] args)
{
// Given array []arr
int[] arr = { 1, 3, 1, 5, 1, 3 };
// Size of array
int N = arr.Length;
int K = 2;
// Function Call
splitArray(arr, N, K);
}
}
// This code is contributed by shikhasingrajput
输出:
4
时间复杂度: O(N logN)
辅助空间: O(N)