📌  相关文章
📜  将数组拆分为 K 个长度的子集,以最小化每个子集的第二小元素的总和

📅  最后修改于: 2021-09-04 08:20:18             🧑  作者: Mango

给定一个大小为N的数组arr[]和一个整数K ( N % K = 0 ),任务是将数组拆分为大小为K 的子数组,使得每个子数组的第二个最小元素的总和尽可能最小。

例子:

方法:该问题可以通过排序技术来解决。请按照以下步骤解决此问题:

  • 按升序对给定数组进行排序。
  • 由于所有子集的长度为K组,因此选择从1开始的前K 个奇数索引元素并计算它们的总和。
  • 打印获得的总和。

下面是上述方法的实现:

C++
// C++ implementation for above approach
 
#include 
using namespace std;
 
// Function to find the minimum sum of
// 2nd smallest elements of each subset
int findMinimum(int arr[], int N, int K)
{
    // Sort the array
    sort(arr, arr + N);
 
    // Stores minimum sum of second
    // elements of each subset
    int ans = 0;
 
    // Traverse first K 2nd smallest elements
    for (int i = 1; i < 2 * (N / K); i += 2) {
 
        // Update their sum
        ans += arr[i];
    }
 
    // Print the sum
    cout << ans;
}
 
// Driver Code
int main()
{
    // Given Array
    int arr[] = { 11, 20, 5, 7, 8,
                  14, 2, 17, 16, 10 };
 
    // Given size of the array
    int N = sizeof(arr)
            / sizeof(arr[0]);
 
    // Given subset lengths
    int K = 5;
 
    findMinimum(arr, N, K);
 
    return 0;
}


Java
// Java implementation for the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find the minimum sum of
    // 2nd smallest elements of each subset
    public static void findMinimum(
        int arr[], int N, int K)
    {
        // Sort the array
        Arrays.sort(arr);
 
        // Stores minimum sum of second
        // elements of each subset
        int ans = 0;
 
        // Traverse first K 2nd smallest elements
        for (int i = 1; i < 2 * (N / K); i += 2) {
 
            // Update their sum
            ans += arr[i];
        }
 
        // Print the sum
        System.out.println(ans);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given Array
        int[] arr = { 11, 20, 5, 7, 8,
                      14, 2, 17, 16, 10 };
 
        // Given length of array
        int N = arr.length;
 
        // Given subset lengths
        int K = 5;
 
        findMinimum(arr, N, K);
    }
}


Python3
# Python3 implementation for above approach
 
# Function to find the minimum sum of
# 2nd smallest elements of each subset
def findMinimum(arr, N, K):
   
    # Sort the array
    arr = sorted(arr)
 
    # Stores minimum sum of second
    # elements of each subset
    ans = 0
 
    # Traverse first K 2nd smallest elements
    for i in range(1, 2 * (N//K), 2):
 
        # Update their sum
        ans += arr[i]
 
    # Prthe sum
    print (ans)
 
# Driver Code
if __name__ == '__main__':
   
    # Given Array
    arr = [11, 20, 5, 7, 8, 14, 2, 17, 16, 10]
 
    # Given size of the array
    N = len(arr)
 
    # Given subset lengths
    K = 5
    findMinimum(arr, N, K)
 
# This code is contributed by mohit kumar 29


C#
// C# implementation for above approach
using System;
  
class GFG{
  
// Function to find the minimum sum of
// 2nd smallest elements of each subset
public static void findMinimum(int[] arr, int N,
                               int K)
{
     
    // Sort the array
    Array.Sort(arr);
 
    // Stores minimum sum of second
    // elements of each subset
    int ans = 0;
 
    // Traverse first K 2nd smallest elements
    for(int i = 1; i < 2 * (N / K); i += 2)
    {
         
        // Update their sum
        ans += arr[i];
    }
 
    // Print the sum
    Console.WriteLine(ans);
}
 
// Driver Code
public static void Main()
{
     
    // Given Array
    int[] arr = { 11, 20, 5, 7, 8,
                  14, 2, 17, 16, 10 };
 
    // Given length of array
    int N = arr.Length;
 
    // Given subset lengths
    int K = 5;
 
    findMinimum(arr, N, K);
}
}
 
// This code is contributed by susmitakundugoaldanga


Javascript


输出:
13

时间复杂度: O(NlogN)
空间复杂度: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live