📌  相关文章
📜  检查第一个数组的所有 K 长度子集和是否大于第二个数组的所有 K 长度子集和

📅  最后修改于: 2021-09-06 06:35:43             🧑  作者: Mango

给定两个大小为N 的数组A[]B[]和一个整数K ,任务是检查数组A[]的大小为K的子集的所有可能子集和是否大于数组B[]或不。如果发现是真的,则打印“YES” 。否则,打印“NO”

例子:

朴素方法:解决此问题的最简单方法是从数组A[] 和 B[]生成大小为K 的所有可能子集,并计算它们各自的总和。检查数组A[] 中获得的所有总和是否超过数组B[]的总和。如果发现是真的,则打印“YES” 。否则,打印“NO”

时间复杂度: O(K × N 2K )
辅助空间: O(N K )

高效方法:为了优化上述方法,这个想法是基于以下事实:如果所述阵列A []的大小为K的最小子集之和大于所述阵列B的大小K []的最大子集之和,然后打印“是” 。否则,打印“NO”。请按照以下步骤解决问题:

  • 种类 数组A[]按升序排列。
  • 按降序对数组B[]进行排序。
  • 遍历数组,并检查阵列A的前K个元素的和[]是比数组B []与否的第一K个元素的总和。如果发现是真的,则打印“YES” 。否则,打印“NO”

下面是上述方法的实现;

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to check all subset-sums
// of K-length subsets in A[] is greater
// that that in the array B[] or not
bool checkSubsetSum(int A[], int B[], int N,
                    int K)
{
    // Sort the array in
    // ascending order
    sort(A, A + N);
 
    // Sort the array in
    // descending order
    sort(B, B + N,
         greater());
 
    // Stores sum of first K
    // elements of A[]
    int sum1 = 0;
 
    // Stores sum of first K
    // elements of B[]
    int sum2 = 0;
 
    // Traverse both the arrays
    for (int i = 0; i < K; i++) {
 
        // Update sum1
        sum1 += A[i];
 
        // Update sum2
        sum2 += B[i];
    }
 
    // If sum1 exceeds sum2
    if (sum1 > sum2) {
        return true;
    }
 
    return false;
}
 
// Driver Code
int main()
{
    int A[] = { 12, 11, 10, 13 };
    int B[] = { 7, 10, 6, 2 };
 
    int N = sizeof(A) / sizeof(A[0]);
 
    int K = 3;
    if (checkSubsetSum(A, B, N, K)) {
        cout << "YES";
    }
    else {
        cout << "NO";
    }
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
    
class GFG{
     
// Function reverses the elements of the array
static void reverse(int myArray[])
{
    Collections.reverse(Arrays.asList(myArray));
}
    
// Function to check all subset-sums
// of K-length subsets in A[] is greater
// that that in the array B[] or not
static boolean checkSubsetSum(int A[], int B[],
                              int N, int K)
{
     
    // Sort the array in
    // ascending order
    Arrays.sort(A);
  
    // Sort the array in
    // descending order
    Arrays.sort(B);
    reverse(B);
  
    // Stores sum of first K
    // elements of A[]
    int sum1 = 0;
  
    // Stores sum of first K
    // elements of B[]
    int sum2 = 0;
  
    // Traverse both the arrays
    for(int i = 0; i < K; i++)
    {
         
        // Update sum1
        sum1 += A[i];
  
        // Update sum2
        sum2 += B[i];
    }
  
    // If sum1 exceeds sum2
    if (sum1 > sum2)
    {
        return true;
    }
    return false;
}
    
// Driver Code
public static void main(String[] args)
{
    int A[] = { 12, 11, 10, 13 };
    int B[] = { 7, 10, 6, 2 };
  
    int N = A.length;
    int K = 3;
     
    if (checkSubsetSum(A, B, N, K))
    {
        System.out.print("YES");
    }
    else
    {
        System.out.print("NO");
    }
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program to implement
# the above approach
 
# Function to check all subset-sums
# of K-length subsets in A[] is greater
# that that in the array B[] or not
def checkSubsetSum(A, B, N, K):
     
    # Sort the array in
    # ascending order
    A.sort()
     
    # Sort the array in
    # descending order
    B.sort(reverse = True)
     
    # Stores sum of first K
    # elements of A[]
    sum1 = 0
     
    # Stores sum of first K
    # elements of B[]
    sum2 = 0
 
    # Traverse both the arrays
    for i in range(K):
         
        # Update sum1
        sum1 += A[i]
         
        # Update sum2
        sum2 += B[i]
         
    # If sum1 exceeds sum2
    if (sum1 > sum2):
        return True
         
    return False
 
# Driver Code
A = [ 12, 11, 10, 13 ]
B = [ 7, 10, 6, 2]
 
N = len(A)
K = 3
 
if (checkSubsetSum(A, B, N, K)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by avanitrachhadiya2155


C#
// C# program to implement
// the above approach
using System;
 
public class GFG{
     
// Function reverses the elements of the array
static void reverse(int []myArray)
{
    Array.Sort(myArray);
    Array.Reverse(myArray);
}
    
// Function to check all subset-sums
// of K-length subsets in []A is greater
// that that in the array []B or not
static bool checkSubsetSum(int []A, int []B,
                              int N, int K)
{
     
    // Sort the array in
    // ascending order
    Array.Sort(A);
  
    // Sort the array in
    // descending order
    Array.Sort(B);
    reverse(B);
  
    // Stores sum of first K
    // elements of []A
    int sum1 = 0;
  
    // Stores sum of first K
    // elements of []B
    int sum2 = 0;
  
    // Traverse both the arrays
    for(int i = 0; i < K; i++)
    {
         
        // Update sum1
        sum1 += A[i];
  
        // Update sum2
        sum2 += B[i];
    }
  
    // If sum1 exceeds sum2
    if (sum1 > sum2)
    {
        return true;
    }
    return false;
}
    
// Driver Code
public static void Main(String[] args)
{
    int []A = { 12, 11, 10, 13 };
    int []B = { 7, 10, 6, 2 };
  
    int N = A.Length;
    int K = 3;
     
    if (checkSubsetSum(A, B, N, K))
    {
        Console.Write("YES");
    }
    else
    {
        Console.Write("NO");
    }
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
YES

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

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