Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I have an array having some interger elements. I have to split into 2 subsets. Need to verify that sum of these 2 subsets are equal or not. Subset size is not matter, it can be element count of 3,2 or 1,4 or any but should be in 2 part. For example...

Ex 1: @a1 = qw(1, 3, 8, 4); This array can be divided into to subsets (1,3,4)=8 and (8)=8, so these two have equal sum.

Ex 2: @a1 = qw(1, 6, 2); This cant be divided into two subsets of equal sum.Because (1,6)!=2 or any combination of a subset is not matching to another subset.

I tried below code but it is working for one set of iteration, actually it should check all possible set of iteration to find possiblities.

my @array= qw(1 3 5 7); my @array= qw(1 3 5 7); &test(\@array); sub test { my ($s1,$s2); my @a=@{$_[0]}; for (my $i=0;$i<=$#a ;$i++) { ($s1,$s2)=0; for (my $j=0;$j<=$#a ;$j++) { if ($i == $j) { $s1=$s1+ $a[$j]; } else{ $s2+=$a[$j]; } } } print "\n"; }

In reply to Divide an array into 2 subsets to verify their sum is equal or not. by bimleshsharma

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (2)
    As of 2018-05-26 10:56 GMT
    Find Nodes?
      Voting Booth?