Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

As for the interesting case of all negative numbers noted by BrowserUK++, coming from the initial "best" intervall being an empty list with sum zero, which will then serve as an upper limit not to surpass in the loop.

I think that can be dealt with by

Update: hiding original approach in readmore tags, since it is incorrect and now obsolete

  • Setting the first element for the best intervall, if the @array is non-empty, prior to entering the loop
  • Swapping the order of the if-elsif tests
  • But then having to check again for $cur < 0 wrt setting $best_start


As in:
# initialisation as before $cur = $best = $array[$cur_start] if @array; for (1..$#array) { $cur += $array[$_]; if ($best < $cur) { $best = $cur; $best_start = $cur < 0 ? $_ : $cur_start; $best_end = $_; } elsif ($cur < 0) { $cur = 0; $cur_start = $_ + 1; } }

(start Update insert)

So instead of messing that much with tilly's code I now concentrate on the "best" initialization before we enter the loop.

For my addendum loops through the beginning of @array as long as we are still seeing negative numbers.

When we finally see a non-negative we proceed with tilly's code from there.

To be prepared for the case that all elements are negative, we adjust the *_start, *_end, best*, cur* variables heading for the max (= least negative number) instead.

Which leaves me with some code not so elegant anymore
Maybe someone else is able to reimplement tilly's original idea also to work for all negative numbers in a more elegant/compact/concise way

#! /usr/bin/perl -w use strict; my @array = @ARGV ? @ARGV : qw(3 2 8 9 -25 5 8 4 4 -3 5 3 -10); my $cur = my $best = my $best_start = my $cur_start = 0; my $best_end = -1; for (0 .. $#array) { my $elem = $array[$_]; if ($elem < 0) { if (!$best or $best <= $elem) { $cur = $best = $elem; $best_start = $best_end = $_; } } else { $cur_start = $best_start = $best_end = $_; $cur = $best = $elem; last; } } for ($best_end+1..$#array) { $cur += $array[$_]; if ($cur < 0) { $cur = 0; $cur_start = $_ + 1; } elsif ($best < $cur) { $best = $cur; $best_start = $cur_start; $best_end = $_; } } print qq(Start: $best_start End: $best_end Sum: $best Numbers: @array[$best_start..$best_end] );

(end update insert)

Otherwise I think the idea behind the algorithm presented by tilly++ is correct, or so I hope.

Update: Still not correct as presented. Back to the drawing board...


In reply to Re^2: Largest Sum of Consecutive Integers by pKai
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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (7)
    As of 2017-12-18 22:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      What programming language do you hate the most?




















      Results (500 votes). Check out past polls.

      Notices?