📌  相关文章
📜  最小化 K 个长度为 L 的子序列中最小元素的总和

📅  最后修改于: 2021-09-03 03:55:05             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是通过从长度为L 的arr[]的任何K个子序列中提取最小元素来找到最小可能的总和,这样每个子序列都没有共享元素。如果无法获得所需的总和,则打印 -1。

例子:

方法:
为了优化上述方法,我们需要观察以下细节:

  • 数组的K 个最小元素有助于找到 K 个子序列的最小元素的最小总和。
  • 数组的长度必须大于或等于 (K * L) 才能形成 K 个长度为 L 的子序列。

请按照以下步骤解决问题:

  • 检查数组arr[]的大小是否大于等于(K * L)
  • 如果是,则对数组arr[]进行排序,并在排序后打印数组的前K 个元素的总和。
  • 否则,返回-1。

下面是上述方法的实现:

C++
// C++ Program to find the minimum
// possible sum of the smallest
// elements from K subsequences
 
#include 
using namespace std;
 
// Function to find the minimum sum
int findMinSum(int arr[], int K,
               int L, int size)
{
 
    if (K * L > size)
        return -1;
 
    int minsum = 0;
 
    // Sort the array
    sort(arr, arr + size);
 
    // Calculate sum of smallest
    // K elements
    for (int i = 0; i < K; i++)
        minsum += arr[i];
 
    // Return the sum
    return minsum;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 15, 5, 1,
                  35, 16, 67, 10 };
    int K = 3;
    int L = 2;
 
    int length = sizeof(arr)
                / sizeof(arr[0]);
 
    cout << findMinSum(arr, K,
                       L, length);
 
    return 0;
}


Java
// Java program to find the minimum
// possible sum of the smallest
// elements from K subsequences
import java.util.Arrays;
 
class GFG{
 
// Function to find the minimum sum
static int findMinSum(int []arr, int K,
                      int L, int size)
{
    if (K * L > size)
        return -1;
 
    int minsum = 0;
 
    // Sort the array
    Arrays.sort(arr);
 
    // Calculate sum of smallest
    // K elements
    for(int i = 0; i < K; i++)
        minsum += arr[i];
 
    // Return the sum
    return minsum;
}
 
// Driver Code
public static void main(String args[])
{
    int arr[] = { 2, 15, 5, 1,
                  35, 16, 67, 10 };
    int K = 3;
    int L = 2;
    int length = arr.length;
 
    System.out.print(findMinSum(arr, K,
                                L, length));
}
}
 
// This code is contributed by Ritik Bansal


Python3
# Python3 program to find the minimum
# possible sum of the smallest
# elements from K subsequences
 
# Function to find the minimum sum
 
 
def findMinSum(arr, K, L, size):
 
    if (K * L > size):
        return -1
 
    minsum = 0
 
    # Sort the array
    arr.sort()
 
    # Calculate sum of smallest
    # K elements
    for i in range(K):
        minsum += arr[i]
 
    # Return the sum
    return minsum
 
 
# Driver code
if __name__ == '__main__':
 
    arr = [2, 15, 5, 1,
           35, 16, 67, 10]
    K = 3
    L = 2
 
    length = len(arr)
 
    print(findMinSum(arr, K, L, length))
 
# This code is contributed by Shivam Singh


C#
// C# program to find the minimum
// possible sum of the smallest
// elements from K subsequences
using System;
   
class GFG{
   
// Function to find the minimum sum
static int findMinSum(int []arr, int K,
                      int L, int size)
{
    if (K * L > size)
        return -1;
   
    int minsum = 0;
   
    // Sort the array
    Array.Sort(arr); 
   
    // Calculate sum of smallest
    // K elements
    for(int i = 0; i < K; i++)
        minsum += arr[i];
   
    // Return the sum
    return minsum;
}
   
// Driver Code
public static void Main() 
{
    int[] arr = { 2, 15, 5, 1,
                  35, 16, 67, 10 };
    int K = 3;
    int L = 2;
    int length = arr.Length;
   
    Console.Write(findMinSum(arr, K,
                             L, length));
}
}
 
// This code is contributed by code_hunt


Javascript


输出
8

时间复杂度: O(N * log(N))
空间复杂度: O(1)

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