Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Largest Sum of Consecutive Integers

by pKai (Priest)
on Aug 31, 2006 at 10:15 UTC ( [id://570573]=note: print w/replies, xml ) Need Help??


in reply to Re: Largest Sum of Consecutive Integers
in thread Largest Sum of Consecutive Integers

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...

Replies are listed 'Best First'.
Re^3: Largest Sum of Consecutive Integers
by tilly (Archbishop) on Aug 31, 2006 at 14:32 UTC
    Speaking mathematically, the sum of the empty set is a perfectly valid sum of consecutive integers, and its value is 0. But you can change the problem to exclude the empty sum as follows:
    #! /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_start = my $best_end = my $cur_start = 0; my $best = $array[0]; for (0..$#array) { $cur += $array[$_]; if ($best < $cur) { $best = $cur; $best_start = $cur_start; $best_end = $_; } if ($cur < 0) { $cur = 0; $cur_start = $_ + 1; } } print qq(Start: $best_start End: $best_end Sum: $best Numbers: @array[$best_start..$best_end] );

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://570573]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-19 13:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found