Check if all K | 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
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 ( " " ) ; } else { document . write ( " " ) ; }