1

I started with a basic counter that starts at 1 and counts to 300:

for( var i = 1; i <= 300; i++ ){
  document.querySelector( 'tbody' ).innerHTML += '<tr><td>' + i + '</td></tr>';
}
html{ font-family:arial } table{ border-collapse:collapse; text-align:center }
td, th{ border:1px solid black; padding:10px }
<table> <thead> <tr> <th>COUNTER</th> </tr> </thead> <tbody></tbody> </table>

I have an array: var ary = [5,10,28,50,56,280]. As stated in the title I want to find the lowest combination of keys in this array resulting in a sum that also remains above a variable I'll call needle.

var needle = 0

I'll explain by example: enter image description here

The next row would look like: | 5 | 25 | 10 + 10 + 5 |.

It's meant to be a simple counter that finds the lowest combination of all keys in an array above the previous value.

I've made some attempts and run into walls every single time. Here was my most recent go at just trying to get all the combinations together but even that fails because it doesn't deal with the possibility of repeated indexs, nor does it increase beyond the max sum of the array:

function add(a, b) { //add all keys in array
    return a + b;
}

var combine = function(a, min) { //find all combinations of array
    var fn = function(n, src, got, all) {
        if (n == 0) {
            if (got.length > 0) {
                all[all.length] = got;
            }
            return;
        }
        for (var j = 0; j < src.length; j++) {
            fn(n - 1, src.slice(j + 1), got.concat([src[j]]), all);
        }
        return;
    }
    var all = [];
    for (var i = min; i < a.length; i++) {
        fn(i, a, [], all);
    }
    all.push(a);
    return all;
}

var subsets = combine([5,10,28,50,56,280], 1);
var limit = 11;

for( var i = 0; i < subsets.length; i++ ){
  document.write('combination: ' + subsets[ i ] + ' sum: ' + subsets[ i ].reduce(add, 0) + '<br>');
}
  • Why would `5+5` not also be a valid answer in the second row? Is there some other constraint? –  Oct 26 '16 at 02:53
  • By "keys" do you mean the values of the elements of the array? – guest271314 Oct 26 '16 at 02:54
  • @torazaburo It's important for this function to use as few addends as possible with each step. I'll update my question. –  Oct 26 '16 at 02:54
  • @guest271314 Yes. By keys I mean Indexes of the array. –  Oct 26 '16 at 02:55
  • Then please add that bit of information to the question. –  Oct 26 '16 at 02:55
  • Indexes or values? – guest271314 Oct 26 '16 at 02:55
  • You seem to be a bit fuzzy on the difference between keys, elements, and indexes. I guess you mean "elements" ("values"). –  Oct 26 '16 at 02:56
  • @guest271314 Yes. Values. Each value inside my array. such as ary[ 0 ] or ary[ 1 ] –  Oct 26 '16 at 02:56
  • @torazaburo I thought *key* was a synonym for *index* in JS arrays. Isn't element also? –  Oct 26 '16 at 02:57
  • Wondering if a program without more overarching logic is really the way to go here. There could get to be so many things to try pretty quickly. But since 5 and 28 are relatively prime, eventually it should go up by 1's. If you want the minimum, this would be the postage stamp problem, I think. – Jeremy Kahan Oct 26 '16 at 02:57
  • You can take the algorithm in http://stackoverflow.com/questions/34658756/find-the-highest-subset-of-an-integer-array-whose-sums-add-up-to-a-given-target and modify it to find combinations whose sum exceeds a target value, instead of equalling it. –  Oct 26 '16 at 02:58
  • @JeremyKahan, The overall plan is to add restraints at certain intervals. This is part of a larger webapp but this cementing this function first is the best way I can think of to implement the larger plan. –  Oct 26 '16 at 02:59
  • Still not sure what expected result is? Add two values of array greater than the current index where the sum is the lowest possible sum of any two values of the remainder of the array? – guest271314 Oct 26 '16 at 02:59
  • In `[42]`, the index (less commonly, "key") is 0, and the element (or value) is 42. –  Oct 26 '16 at 03:01
  • @guest271314 The desired result is to generate values greater than the previous value of `needle` each iteration. These values can only be derived from combinations of the indexes inside the array. These values must be the smallest possible value. –  Oct 26 '16 at 03:02
  • _"These values can only be derived from combinations of the indexes inside the array."_ Do you mean indexes or values? – guest271314 Oct 26 '16 at 03:04
  • @torazaburo also the meaning of everything. Thanks for the clarification. –  Oct 26 '16 at 03:04
  • _"nor does it increase beyond the max sum of the array"_ How would that be possible? – guest271314 Oct 26 '16 at 03:07
  • @guest271314 I mean the value of the variable `needle` can only be derived from the value inside `ary[ x ]`. So not `x` but the value inside of `x`. In my example `ary[ 0 ] = 5`. So the value of `needle` would be derived from **5** among the other values...I'm working on a lot of editing my question now. –  Oct 26 '16 at 03:08
  • Do you mean increment `needle` by the value of the current index of the array? The graph at OP increments `needle` by a constant `5` – guest271314 Oct 26 '16 at 03:11
  • @guest271314 In **my** attempt, the combinations I generate don't exceed beyond the max sum of the array: 5 + 10 + 28 + 50 + 56 + 280 = 429. Thats the highest I got in **my** example but I want it my function to able to exceed this... –  Oct 26 '16 at 03:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126681/discussion-between-carb0nshel1-and-guest271314). –  Oct 26 '16 at 03:13
  • _"I want it to be able to exceed this"_ By what means? Adding elements to the array? – guest271314 Oct 26 '16 at 03:13
  • @guest271314 click on the chat link so we can discuss this further. Thanks already for pointing out so much. –  Oct 26 '16 at 03:14
  • http://chat.stackoverflow.com/rooms/126681/discussion-between-carb0nshel1-and-guest271314 –  Oct 26 '16 at 03:16

0 Answers0