Check if all K | C ++ 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 ; Sort the array in ascending order ; Sort the array in descending order ; Stores sum of first K elements of A [ ] ; Stores sum of first K elements of B [ ] ; Traverse both the arrays ; Update sum1 ; Update sum2 ; If sum1 exceeds sum2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ; }