Split array two array based on sum

Certified Lead Developer

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

  • What rules govern the split? Are you looking for all permutations (possible ways) of making a total of 20 or less? Using the numbers in the initial array once or as many times as you like? e.g. {10,5}, {10,6}, {10,9} etc.

  • 0
    Certified Lead Developer
    in reply to Stewart Burchell

    Hi,
    each number can be used only one time.

  • Not sure if this is what you're looking for but the following code takes each item in the array and looks ahead across the array for a second number that when added to it is less than or equal to 20:

    a!localVariables(
      local!nums: { 10, 5, 6, 9, 8, 11, 7, 4, 2, 13, 16, 1, 5 },
      local!indexes: fn!enumerate(fn!length(local!nums)) + 1,
      /* */
      local!reducedArrays: a!forEach(
        items: local!indexes,
        expression: if(
          fv!isLast,
          local!nums,
          fn!remove(local!nums, fn!enumerate(fv!item) + 1)
        )
      ),
      fn!reject(
        a!isNullOrEmpty,
        a!forEach(
          items: local!reducedArrays,
          expression: a!localVariables(
            local!currentArray: fv!item,
            a!forEach(
              items: local!currentArray,
              expression: if(
                fv!isFirst,
                null,
                if(
                  local!currentArray[1] + fv!item <= 20,
                  a!map(
                    firstNumber: local!currentArray[1],
                    secondNumber: fv!item
                  ),
                  null
                )
              )
            )
          )
        )
      )
    )

    If you're looking to see if you can take each number in turn and see if you can add one or more numbers from the rest of the array then that'll be a different solution.

  • 0
    Certified Lead Developer
    in reply to Stewart Burchell

    Thanks,
    but the lenght of subarray can be more than 2.
    For this input:
    { 10, 5, 6, 9, 8, 11, 7, 4, 2, 13, 16, 1, 5 }
    I want this output
    {{10,5},{6,9},{8,11},{7, 4, 2},{13},{16,1},{5}}

  • 0
    Certified Lead Developer

    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]
        )
    )