Search code examples
javascriptarraysconditional-statementssum

How can I add an array of numbers recursively under the condition that the sum will not be more than 20?


For example, there is an array:

[3, 8, 9, 2, 7, 5, 6, 5, 3, 11, 9, 17, 6, 5, 8, 4, 2, 7, 9, 12, 5, 16, 4]

Algorithm:

let arr = []

3 + 8 + 9 + 2 > 20 (do not sum)
3 + 8 + 9 = 20 (sum and push to arr)
next
9 + 2 + 7 + 5 > 20 (do not sum)
9 + 2 + 7 < 20 (sum and push to arr)
next
5 + 6 + 5 + 3 + 11 > 20 (do not sum)
5 + 6 + 5 + 3 < 20 (sum and push to arr)
...

The point is that there are files in the array with a certain weight in MB, and I need to merge them one by one, not exceeding 20MB, and make a new array from the merged ones.

I tried to do this with the following code, but I don't understand how to set my condition.

var sum = (array) => (array.length === 0) ? 0 : array[0] + sum(array.slice(1));

Solution

  • You could group by having a look to subset sum.

    const
        add = (a, b) => a + b,
        group = compare => (r, v, i) => {
            if (!i || compare(r.at(-1).reduce(add) + v)) r.push([v]);
            else r.at(-1).push(v);
            return r;
        },
        upTo = a => b => a <= b,
        lessThan = a => b => a < b,
        data = [3, 8, 9, 2, 7, 5, 6, 5, 3, 11, 9, 17, 6, 5, 8, 4, 2, 7, 9, 12, 5, 16, 4],
        result0 = data.reduce(group(lessThan(20)), []);
        result1 = data.reduce(group(upTo(20)), []);
    
    console.log(result0);
    console.log(result1);
    .as-console-wrapper { max-height: 100% !important; top: 0; }