Hi,

I am trying to divide an array of integers into the two subarrays whose difference of the sums is the minimum possible. It would be something like having balls with different weights and trying to split them in two groups that will balance best in a two plate scale. For example,

my @array = (8,14,32,29);
should be divided into
@subarray1 = (8,32) # total value 40
@subarray2 = (14,29) # total value 43

or

my @array = (7,10,12,15,40);
should be divided into
@subarray1 = (7,10,12,15) # add up to 44
@subarray2 = (40) #add up to 40

Any suggestions? I don't seem to find the right method, no matter how hard I try.
Thanks a lot in advance.

Pepe

In reply to Divide array of integers into most similar value halves by Pepe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.