in reply to sum of continues array
Did you just spell "to" as "2"? And what's a "maximum sum"? Anyway,
oruse List::Util qw( sum ); my $sum_of_pos = sum grep { $_ > 0 } @array;
my $sum_of_pos = 0; for (@array) { $sum_of_pos += $_ if $_ > 0; }
Update: Oops, missed the "continues". Maybe that's a misspelling of "contiguous", and maybe you mean "contiguous and increasing"???
my $prev; my $sum; my $max = -1; for (@array) { if (defined($prev) && $_ == $prev+1) { $prev = $_; $sum += $_; next; } if (defined($prev)) { $max = $sum if $sum > $max; undef $prev; undef $sum; } #if ($_ > 0) { # zero isn't considered positive if ($_ >= 0) { # zero is considered positive $prev = $_; $sum = $_; } }
|
|---|