Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-23 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found