Fix indices

This commit is contained in:
Antti H S Laaksonen 2017-05-28 10:40:54 +03:00
parent e4accb5635
commit 1ee57c911b
1 changed files with 9 additions and 9 deletions

View File

@ -411,10 +411,10 @@ The following code implements this algorithm:
\begin{lstlisting} \begin{lstlisting}
int best = 0; int best = 0;
for (int first = 0; first < n; first++) { for (int a = 0; a < n; a++) {
for (int last = first; last < n; last++) { for (int b = a; b < n; b++) {
int sum = 0; int sum = 0;
for (int k = first; k <= last; k++) { for (int k = a; k <= b; k++) {
sum += array[k]; sum += array[k];
} }
best = max(best,sum); best = max(best,sum);
@ -423,9 +423,9 @@ for (int first = 0; first < n; first++) {
cout << best << "\n"; cout << best << "\n";
\end{lstlisting} \end{lstlisting}
The variables \texttt{first} and \texttt{last} determine the range The variables \texttt{a} and \texttt{b} fix the first and
of the subarray, last index of the subarray,
and the sum of the numbers is calculated to the variable \texttt{sum}. and the sum of values is calculated to the variable \texttt{sum}.
The variable \texttt{best} contains the maximum sum found during the search. The variable \texttt{best} contains the maximum sum found during the search.
The time complexity of the algorithm is $O(n^3)$, The time complexity of the algorithm is $O(n^3)$,
@ -442,10 +442,10 @@ The result is the following code:
\begin{lstlisting} \begin{lstlisting}
int best = 0; int best = 0;
for (int first = 0; first < n; first++) { for (int a = 0; a < n; a++) {
int sum = 0; int sum = 0;
for (int last = first; last < n; last++) { for (int b = a; b < n; b++) {
sum += array[last]; sum += array[b];
best = max(best,sum); best = max(best,sum);
} }
} }