Checking for all possibility
var set1 = i ; var set2 = K - i ; if ( greaterE . length >= set1 && lessE . length >= set2 ) { var step1 = 0 ; var step2 = 0 ; for ( var j = 0 ; j < set1 ; j ++ ) step1 += greaterE [ j ] ; for ( var j = 0 ; j < set2 ; j ++ ) step2 += lessE [ j ] ; mi = Math . min ( mi , Math . max ( step1 , step2 ) ) ; } } document . write ( mi ) ; }