<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.appian.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Split array two array based on sum</title><link>https://community.appian.com/discussions/f/general/34690/split-array-two-array-based-on-sum</link><description>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</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: Split array two array based on sum</title><link>https://community.appian.com/thread/133580?ContentTypeID=1</link><pubDate>Tue, 16 Apr 2024 19:03:35 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:9ea2ed92-83fd-4ae7-931b-48507716688d</guid><dc:creator>Dan Lluhi</dc:creator><description>&lt;p&gt;You could probably do this using recursion assuming that your array of numbers doesn&amp;#39;t get too long, and if you&amp;#39;re worried about recursion then usually there&amp;#39;s a way to replicate recursion using the fn!reduce function... but the logic would be something like this pseudocode:&lt;/p&gt;
&lt;p&gt;inputs: A (set of solution arrays), B (current solution array that we are building), C (remaining numbers to process)&lt;/p&gt;
&lt;p&gt;initiated by calling rule!recursion(A: {}, B: {}, C:&amp;nbsp;&lt;span&gt;{ 10, 5, 6, 9, 8, 11, 7, 4, 2, 13, 16, 1, 5 })&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;if(
    isNullOrEmpty(C),
    {A,B},
    if( 
        sum(B, C[1]) &amp;gt; 20, 
        rule!recursion( A: {A,B}, B: {}, C: C[2:end]),
        rule!recursion( A: A, B: {B, C[1}, C: C[2:end]
    )
)&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Split array two array based on sum</title><link>https://community.appian.com/thread/133553?ContentTypeID=1</link><pubDate>Tue, 16 Apr 2024 13:57:26 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:1765f7c6-3df2-42f4-a29c-cd5a003aa988</guid><dc:creator>Giuseppe Leo</dc:creator><description>&lt;p&gt;Thanks,&lt;br /&gt;but the lenght of subarray can be more than 2.&lt;br /&gt;For this input:&lt;br /&gt;{ 10, 5, 6, 9, 8, 11, 7, 4, 2, 13, 16, 1, 5 }&lt;br /&gt;I want this output&lt;br /&gt;{{10,5},{6,9},{8,11},{7, 4, 2},{13},{16,1},{5}}&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Split array two array based on sum</title><link>https://community.appian.com/thread/133552?ContentTypeID=1</link><pubDate>Tue, 16 Apr 2024 13:48:43 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:abc84d2d-ed93-4aa1-8ae9-22fb95a14151</guid><dc:creator>Stewart Burchell</dc:creator><description>&lt;p&gt;Not sure if this is what you&amp;#39;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:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;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 &amp;lt;= 20,
              a!map(
                firstNumber: local!currentArray[1],
                secondNumber: fv!item
              ),
              null
            )
          )
        )
      )
    )
  )
)&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;If you&amp;#39;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&amp;#39;ll be a different solution.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Split array two array based on sum</title><link>https://community.appian.com/thread/133550?ContentTypeID=1</link><pubDate>Tue, 16 Apr 2024 12:52:16 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:1edcc4b4-20e4-4d77-b4a4-509ba0995544</guid><dc:creator>Giuseppe Leo</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;each number can be used only one time.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Split array two array based on sum</title><link>https://community.appian.com/thread/133549?ContentTypeID=1</link><pubDate>Tue, 16 Apr 2024 12:49:19 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:e799f7c5-f33f-4c4e-9b1d-a84a870f28a6</guid><dc:creator>Stewart Burchell</dc:creator><description>&lt;p&gt;What&amp;nbsp;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.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>