From 2755a634d803d08f90629190ddad0d370cb1f570 Mon Sep 17 00:00:00 2001 From: Nikita Zhiltsov Date: Sun, 17 Nov 2019 16:15:07 -0800 Subject: [PATCH] Fix for max subarray sum --- chapter02.tex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chapter02.tex b/chapter02.tex index d062d67..aa74a43 100644 --- a/chapter02.tex +++ b/chapter02.tex @@ -487,8 +487,8 @@ for each ending position from left to right. The following code implements the algorithm: \begin{lstlisting} -int best = 0, sum = 0; -for (int k = 0; k < n; k++) { +int best = array[0], sum = array[0]; +for (int k = 1; k < n; k++) { sum = max(array[k],sum+array[k]); best = max(best,sum); }