The solution you described is O(@array ** 2) - it's complexity is proportional to the square of the number of items in the array. I think it can be done in O(@array) instead - but perhaps I have a mistake in my logic? Sample code follows:
#!/usr/bin/perl -w use strict; # http://perlmonks.org/index.pl?node_id=570420 my @array; if (@ARGV) { @array = @ARGV; } else { @array = qw( 3 2 8 9 -25 5 8 4 4 -3 5 3 -10 ); } my $start = -1; my @groups; my $cursign = 0; foreach (@array) { $start ++; next unless $_; if (($_ > 0 and $cursign == 1) or ($_ < 0 and $cursign == -1)) { $groups[-1]->{sum} += $_; $groups[-1]->{end} = $start; } elsif ($cursign != 0) { $cursign *= -1; push @groups, {sum => $_, start => $start, end => $start}; } else { $cursign = ($_ > 0 ? 1 : -1); push @groups, {sum => $_, start => $start, end => $start}; } } # if the first or last set is negative, just drop it if ($groups[-1]->{sum} < 0) { pop @groups; } if ($groups[0]->{sum} < 0) { shift @groups; } my ($cur_start, $cur_total); my ($best_start, $best_total, $best_end) = (0, 0, 0); while (my $group = shift @groups) { $cur_start = $group->{start} unless defined $cur_start; if ($group->{sum} + $cur_total < 0) { if ($cur_total > $best_total) { $best_total = $cur_total; $best_start = $cur_start; $best_end = $group->{start}-1; } $cur_total = 0; $cur_start = undef; next; } $cur_total += $group->{sum}; if ($cur_total > $best_total) { $best_total = $cur_total; $best_start = $cur_start; $best_end = $group->{end}; } print "$group->{sum}, $cur_total, $best_total\n"; } $best_end = $#array unless defined $best_end; print "$best_total $best_start - $best_end\n";

Basically, it's similar to what you added at the end - go over the array, adding up the items until you find one that just cancels them out. Then start over from that point. Does anyone spot a mistake here?

-- zigdon


In reply to Re: Largest Sum of Consecutive Integers by zigdon
in thread Largest Sum of Consecutive Integers by OverlordQ

Title:
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, details, 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, summary, 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.