sub find_share { my ($target, $treasures) = @_; return [] if $target == 0; # return 0 if $target == 0; #my code experiment # return '' if $target == 0; #my code experiment # return '0' if $target == 0; #my code experiment # return () if $target == 0; #my code experiment # return undef if $target == 0;#my code experiment return if $target < 0 || @$treasures == 0; my ($first, @rest) = @$treasures; my $solution = find_share($target-$first, \@rest); return [$first, @$solution] if $solution; return find_share($target , \@rest); } sub partition { my $total = 0; my $share_2; for my $treasure (@_) { $total += $treasure; } my $share_1 = find_share($total/2, [@_]); return unless defined $share_1; my %in_share_1; for my $treasure (@$share_1) { ++$in_share_1{$treasure}; } for my $treasure (@_) { if ($in_share_1{$treasure}) { --$in_share_1{$treasure}; } else { push @$share_2, $treasure; } } ## My code to run samples my ($rslt1,$rslt2) = partition ( 1,5,3,1,6,2 ); if ($rslt1){ printf "rslt1= %s\n",join ',',@$rslt1; printf "rslt2= %s\n",join ',',@$rslt2; } #### my $share_1 = find_share($total/2, [@_]);