Maximum subarray problem: Difference between revisions

Content deleted Content added
I'm trying to be reasonable, and not condescending like others. It's wrong to place a src change and say it's broken for this scenario when we can easily have it work for all the scenarios. There was never a misunderstanding. Empty array will work as well. More changes will be needed as there are other fallacies I'm noticing.
Tag: Reverted
Minor typing change
Tag: Reverted
Line 58:
In the Python code below, <math>A[j]</math> is expressed as <code>x</code>, with the index <math>j</math> left implicit.
}}
is the maximum over all <math>i \in \{ 1,\ldots, j \}</math> of the sum <math>A[i]+\cdots+A[j]</math>. To extend the latter maximum to cover also the case <math>i=j+1</math>, it is sufficient to consider also the empty subarray <math>A[j+1 \; \ldots \; j]</math>. This is done in line 11 by assigning <math>\max(</math><code>best_sum</code><math>,</math><code>current_sum</code><math>+A[j])</math> as the new value of <code>current_sum</code>, which after that holds the maximum over all <math>i \in \{ 1, \ldots, j+1 \}</math> of the sum <math>A[i]+\cdots+A[j]</math>.
 
Thus, the problem can be solved with the following code,{{sfn|Bentley|1989|p=74}}{{sfn|Gries|1982|p=211}} expressed here in [[Python (programming language)|Python]]: