# Split array two array based on sum

Hi,

i have an array of numbers like this
{ 10, 5, 6, 9, 8, 11, 7, 4, 2, 13, 16, 1, 5 }
I want split this array in several arrays in wich the sum of all element must be maximum 20.
There is any snippet for doing this easily?
Thanks

Discussion posts and replies are publicly visible

Parents
• You could probably do this using recursion assuming that your array of numbers doesn't get too long, and if you're worried about recursion then usually there's a way to replicate recursion using the fn!reduce function... but the logic would be something like this pseudocode:

inputs: A (set of solution arrays), B (current solution array that we are building), C (remaining numbers to process)

initiated by calling rule!recursion(A: {}, B: {}, C: { 10, 5, 6, 9, 8, 11, 7, 4, 2, 13, 16, 1, 5 })

```if(
isNullOrEmpty(C),
{A,B},
if(
sum(B, C[1]) > 20,
rule!recursion( A: {A,B}, B: {}, C: C[2:end]),
rule!recursion( A: A, B: {B, C[1}, C: C[2:end]
)
)```

• You could probably do this using recursion assuming that your array of numbers doesn't get too long, and if you're worried about recursion then usually there's a way to replicate recursion using the fn!reduce function... but the logic would be something like this pseudocode:

inputs: A (set of solution arrays), B (current solution array that we are building), C (remaining numbers to process)

initiated by calling rule!recursion(A: {}, B: {}, C: { 10, 5, 6, 9, 8, 11, 7, 4, 2, 13, 16, 1, 5 })

```if(
isNullOrEmpty(C),
{A,B},
if(
sum(B, C[1]) > 20,
rule!recursion( A: {A,B}, B: {}, C: C[2:end]),
rule!recursion( A: A, B: {B, C[1}, C: C[2:end]
)
)```

Children
No Data