Unfortunately, this algorithm can fail. I didn't turn up an example with small numbers after some thinking, so here's one that I stole from an article transitively linked by Skeeve: the set {62, 83, 121, 281, 486, 734, 771, 854, 885, 1003} has a perfect partition (namely, {62, 83, 121, 486, 885, 1003}), but the greedy algorithm that you suggest returns {1003, 885, 734}, which has a defect of 18 (not 32, I think, despite the article).

The algorithm I used to find that match is based on my imperfect recall of the one from Dominus's book mentioned by moritz. I'm sure it can be made much more elegant (for example, by not hard-wiring @list; and, less trivially, by not passing in $so_far_ref—I was just using it to print the diagnostics), but I think that this works:

use List::Util qw/first sum/; use strict; use warnings 'all'; my @list = (62, 83, 121, 281, 486, 734, 771, 854, 885, 1003); my $sum = sum(@list)/2; print "Trying for $sum with possibilities @list\n"; my @match = sum_to($sum, [], \@list); print @match ? "MATCH @match\n" : "No match\n"; sub sum_to { my ( $target, $so_far_ref, $possibilities_ref ) = @_; my @so_far = @$so_far_ref; my @possibilities = @$possibilities_ref; local $_; my $exact = first { $_ == $target } @possibilities; return $exact if $exact; local $_; my @indices = grep { $possibilities[$_] <= $target } 0 .. $#po +ssibilities; unless ( @indices ) { print "That branch didn't work.\n"; return; } my ( $sum, $try, @match ); INDEX: for my $index ( @indices ) { my @internal_possibilities = @possibilities; my @internal_so_far = @so_far; $try = splice @internal_possibilities, $index, 1; push @internal_so_far, $try; print "Guessed @internal_so_far; recursively trying fo +r ", $target - $try, " with possibilities @internal_possibilities\n"; @match = sum_to($target - $try, \@internal_so_far, \@i +nternal_possibilities) and last INDEX; } return @match ? ( $try, @match ) : (); }

In reply to Re^2: Divide array of integers into most similar value halves by JadeNB
in thread 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.