Given an array a1,a2,…,an of integers (possibly negative), find the maximum sum of any contiguous subarray ai+ai+1+⋯+aj for 1≤i≤j≤n.
For example, for [−2,1,−3,4,−1,2,1,−5,4] the answer is 6 (subarray [4,−1,2,1]).
Give an O(n) algorithm.