Skip to main content
New Participant
April 16, 2024
Question

Split array two array based on sum

  • April 16, 2024
  • 5 replies
  • 0 views

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

5 replies

stewart.burchell
April 16, 2024

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.

New Participant
April 16, 2024

Hi,
each number can be used only one time.

stewart.burchell
April 16, 2024

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.

daniell
Participating Frequently
April 16, 2024

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