Re: Minimum sub-sequence sum



In article
<9b537cf5-5864-499c-8da0-d9e9c442f438@xxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Michael Jarrod <Michael.Jarrod@xxxxxxxxx> wrote:

I have a sequence of integers (positive/negative values) and i would
like to find the sub sequence of consecutive values that yields the
smallest sum, I can think of a very simple O(n^3) complex solution,
however I was wondering are there any better solutions? and what is
the name of this kind of problem?

Call your integers a_1, a_2, ..., a_n.

Compute the numbers
s_1 = a_1,
s_2 = a_1 + a_2,
....
s_n = a_1 + a_2 + ... + a_n.

That much should only take O(n) operations.

Now calculate all the numbers s_i - s_j with i > j,
and keep track of which of them is the smallest.
That's O(n^2), and you're done.

--
Gerry Myerson (gerry@xxxxxxxxxxxxxxx) (i -> u for email)
.



Relevant Pages

  • Re: Minimum sub-sequence sum
    ... like to find the sub sequence of consecutive values that yields the ... smallest sum, I can think of a very simple Ocomplex solution, ...
    (comp.programming)
  • Re: Minimum sub-sequence sum
    ... like to find the sub sequence of consecutive values that yields the ... smallest sum, I can think of a very simple Ocomplex solution, ... subquadratic algorithms as well as two quadratic algorithms he ...
    (comp.theory)
  • Minimum sub-sequence sum
    ... like to find the sub sequence of consecutive values that yields the ... smallest sum, I can think of a very simple Ocomplex solution, ...
    (comp.programming)
  • Minimum sub-sequence sum
    ... like to find the sub sequence of consecutive values that yields the ... smallest sum, I can think of a very simple Ocomplex solution, ...
    (comp.theory)
  • Minimum sub-sequence sum
    ... like to find the sub sequence of consecutive values that yields the ... smallest sum, I can think of a very simple Ocomplex solution, ...
    (sci.math)