Check if all K | Java program to implement the above approach ; Function reverses the elements of the array ; 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
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 " ) ; } } }