Given an array var ary = [5,10,28,50,56,280]
, I am exploring the idea of generating all possible combinations or subsets of this array until a certain threshold is reached.
The objective is to store this limit in a variable and collect all the elements being added into an array.
var ary = [5,10,28,50,56,280];
var limit = 11;
var result = [];
var addends = [];
for( var i = 0; i <= ary.length; i++ ){
if( ary[ i ] > limit ){
result.push( ary[ i ] );
addends.push( ary[ i ] );
}
else if( ary[ i ] + ary[ i ] > limit ){
result.push( ary[ i ] + ary[ i ] )
addends.push( ary[ i ] );
addends.push( ary[ i ] );
}
}
Is there a systematic approach that can be applied regardless of the size of the array or the value of limit
?