Given an array with integers, find the largest sum of continuous subarray. This maximum subarray problem was first posed in 1977. Jay Kadane solved it in linear time (O(n)) in 1988.

The Approach

The brute-force way of solving this is to use 2 for-each loops, one inside another, to loop through all combination. However, the linear approach is to use dynamic programming to break down the problem into small ones.

Characteristics of dynamic programming:

Optimal substructure - The solution to a problem in a bigger set can be efficiently represented in terms of a solution to a problem in an incrementally smaller set. That is, problem can be divided into stages.

Recursive nature of the solution.

Bottom-Up (solve smaller problems first)

Thought Process

The bigger problem is to find all the combination of contiguous subarrays of a given array A. Among all the combination of contiguous subarrays, find the maximum sum. A smaller problem is to find the maximum sum of contiguous subarray of a given smaller array.

When you add an element to a smaller array, "the max of possible sub sequence sum of the bigger array that includes the last element as part of the sum" is either the added element itself, or "the previous max of possible sub sequence sum of the bigger array that includes the previous added element as part of the sum"plus the newly added element. The recursive nature of the solution is:

m(i) = the max of possible sub sequence sum of the array that includes the last element as part of the sum = max{m(i-1)+A[i], A[i]}