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

Parents Reply Children
  • 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}}