perlquestion
bimleshsharma
<p>
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...
</p>
<p>
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.
</p>
<p>
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.
</p>
<p>
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.
</p>
<code>
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";
}
</code>