# How do I find the largest continuous sum in a list? Kadane's Algorithm is the answer.

## The Problem

You are presented with an unordered list of numbers. What is the largest sum in this list you can find by only adding contiguous numbers, meaning no gap in the series?

## Need an Introduction to Algorithms and Big O Notation?

- Computer Science: An Introduction to Algorithms and Big O Notation

Many computer science students and self-taught programmers find Big O notation to be intimidating. This simple breakdown will shed some light on Big O notation and what it means for programmers.

## Analysis

This problem is a simple problem typically given to computer science students in their early years. We are given an unordered list of data, which means we must scan each element at least once. The best complexity we can possible achieve will be O(n). Is this possible?

If we know how many elements are in the list, we could add each element and find the list sum. Would this be the largest sum in the list? This would be true only if all the elements in the list are a positive value. Any negative values would bring the sum down. Does this mean we need to eliminate any negative values? No, if considering a list such as { 5, -1, 10, -11 }. In this example, the answer would be adding elements one through three, which would result in 14.

What we know so far:

1. We need to find a sum using contiguous numbers in an unordered list that is larger than the sum of any other combination of contiguous numbers.

2. The list is unordered, thus each element needs to be scanned at least once.

3. Just adding every element in the list is not correct when negative values are entered.

4. Eliminating negative numbers is not correct if surrounded by larger positive values.

## The Algorithm

This problem was solved by an algorithm designed by Joseph Kadane of Carnegie Mellon University. It runs with a time complexity of O(n), which is the best possible scenario considering the problem constraints.

In order to solve the problem, we need to consider each element. We can easily see what the sum is ending at this element's position by adding the element to the previous sum. The issue is where is the starting point? We already determined that negative numbers can be included if they are surrounded by larger positive values. If adding a negative number to the sum results in a sum larger than the neighboring element, we can keep that element in our contiguous list, otherwise we set the neighboring element as a new starting point. Doing this, we can end up having to keep track of several contiguous sums, but we only care about keeping track of one at a time and what the maximum value is. Therefore, at a minimum, we need to keep track of two different values. What is the sum of the current contiguous list, and what is the maximum sum of all contiguous lists?

The algorithm:

1. Establish a variable to keep track of the current contiguous list. We will refer to this variable as sum.

2. Establish a variable to keep track of the maximum contiguous list. We will refer to this variable as maxSum.

3. Set sum and maxSum equal to the first element of the list.

4. Iterate through each element from the second element to the final element. Return to this step for each iteration.

5. Add the nth element to sum, and compare the result to sum. If the new result is larger than the current element, set sum to the new result. Otherwise, set sum to the current element.

6. If the value of sum is larger than the value of maxSum, set maxSum to sum.

7. Return to step 4 for the next element. If there are no final elements, return max as the answer.

## Examples

Let's perform the algorithm on the list: { 5, -1, 10, -11 }

First element: 5

Value of sum: 5

Value of max: 5

Second element: -1

Value of (sum + n): 4

Value of sum: 4

Value of max: 5

Third element: 10

Value of (sum + n): 14

Value of sum: 14

Value of max: 14

Fourth element: -11

Value of (sum + n): 3

Value of sum: 3

Value of max: 14

Return value: 14

As we can see, the algorithm returns the correct result from the small list of items we analyzed at the beginning of this problem. Let's try another, more complex list: { 2, -4, -6, 9, 8, -11, 10, 2, -20 }

First element: 2

Value of sum: 2

Value of max: 2

Second element: -4

Value of (sum + n): -2

Value of sum: -2

Value of max: 2

Third element: -6

Value of (sum + n): -8

Value of sum: -6 (-6 is larger than -8, sum is set to -6)

Value of max: 2

Fourth element: 9

Value of (sum + n): 3

Value of sum: 9 (9 is larger than 3, sum is set to 9)

Value of max: 9

Fifth element: 8

Value of (sum + n): 17

Value of sum: 17

Value of max: 17

Sixth element: -11

Value of (sum + n): 6

Value of sum: 6

Value of max: 17

Seventh element: 10

Value of (sum + n): 16

Value of sum: 16

Value of max: 17

Eighth element: 2

Value of (sum + n): 18

Value of sum: 18

Value of max: 18

Ninth element: -20

Value of (sum + n): -2

Value of sum: -2

Value of max: 18

The answer given is 18, which is the sum of the fourth element through the eighth element in the list.

## The Code: C++

Here is an example of a single function that runs Kadane's Algorithm in C++. The only parameter passed to the function is the number of elements that need to be input. The algorithm then follows the steps of the algorithm while scanning each element once. If no elements are provided, then -1 is returned as an error. It is assumed that there are no input errors that will conflict with the variable types. It is also assumed that there will be no overflow.

#include <iostream> #include <cmath> using namespace std; int LargestContiguousSum(int numElements) { if (numElements < 1) return -1; int num; cin >> num; int sum = num, maxSum = num; for (int x = 1; x < numElements; x++) { cin >> num; sum = max(num, sum + num); maxSum = max(maxSum, sum); } return maxSum; }