Given two arrays A[] and B[] of size N and an integer K, the task is to check if all possible subset-sums of subsets of size K of the array A[] are greater than that of the array B[] or not. If found to be true, then print “YES”. Otherwise, print “NO”.
Examples:
Input: A[] = {12, 11, 10, 13}, B[] = {7, 10, 6, 2}, K = 3
Output: YES
Explanation: All possible subset sum of size K(= 3) in A[] are {33, 36, 35, 34}.
All possible subset sum of size K(= 3) in B[] are {23, 19, 15, 18}.
Since all subset-sums of size K in the array A[] is greater than all possible subset-sums of size K in the array B[], the required output is “YES”.
Input : A[] = {5, 3, 3, 4, 4, 6, 1}, B[] = {9, 10, 9, 8, 4, 6, 2}, K = 6
Output : NO
Naive Approach: The simplest approach to solve this problem is to generate all possible subsets of size K from the arrays A[] and B[] and calculate their respective sums. Check if all the sums obtained in array A[] exceeds that of array B[] or not. If found to be true, then print “YES”. Otherwise, print “NO”.
Time Complexity: O(K × N2K)
Auxiliary Space: O(NK)
Efficient Approach: To optimize the above approach, the idea is based on the fact that if the smallest subset-sum of size K of the array A[] is greater than the largest subset-sum of size K of the array B[], then print “YES”. Otherwise, print “NO”. Follow the steps below to solve the problem:
Below is the implementation of the above approach;
C++
#include <bits/stdc++.h>
using namespace std;
bool checkSubsetSum( int A[], int B[], int N,
int K)
{
sort(A, A + N);
sort(B, B + N,
greater< int >());
int sum1 = 0;
int sum2 = 0;
for ( int i = 0; i < K; i++) {
sum1 += A[i];
sum2 += B[i];
}
if (sum1 > sum2) {
return true ;
}
return false ;
}
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
import java.util.*;
class GFG{
static void reverse( int myArray[])
{
Collections.reverse(Arrays.asList(myArray));
}
static boolean checkSubsetSum( int A[], int B[],
int N, int K)
{
Arrays.sort(A);
Arrays.sort(B);
reverse(B);
int sum1 = 0 ;
int sum2 = 0 ;
for ( int i = 0 ; i < K; i++)
{
sum1 += A[i];
sum2 += B[i];
}
if (sum1 > sum2)
{
return true ;
}
return false ;
}
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" );
}
}
}
|
Python3
def checkSubsetSum(A, B, N, K):
A.sort()
B.sort(reverse = True )
sum1 = 0
sum2 = 0
for i in range (K):
sum1 + = A[i]
sum2 + = B[i]
if (sum1 > sum2):
return True
return False
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" )
|
C#
using System;
public class GFG{
static void reverse( int []myArray)
{
Array.Sort(myArray);
Array.Reverse(myArray);
}
static bool checkSubsetSum( int []A, int []B,
int N, int K)
{
Array.Sort(A);
Array.Sort(B);
reverse(B);
int sum1 = 0;
int sum2 = 0;
for ( int i = 0; i < K; i++)
{
sum1 += A[i];
sum2 += B[i];
}
if (sum1 > sum2)
{
return true ;
}
return false ;
}
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" );
}
}
}
|
Javascript
<script>
function checkSubsetSum(A, B, N, K)
{
A.sort((a,b)=> a-b);
B.sort((a,b)=>b-a);
var sum1 = 0;
var sum2 = 0;
for ( var i = 0; i < K; i++) {
sum1 += A[i];
sum2 += B[i];
}
if (sum1 > sum2) {
return true ;
}
return false ;
}
var A = [12, 11, 10, 13];
var B = [7, 10, 6, 2];
var N = A.length;
var K = 3;
if (checkSubsetSum(A, B, N, K)) {
document.write( "YES" );
}
else {
document.write( "NO" );
}
</script>
|
Time Complexity: O(N * log(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
10 May, 2022
Like Article
Save Article