Largest Contiguous Sum in a array
In an array of integers, find the contiguous sequences with largest sum.
Fact:
- If sum is positive then continue to add more element else reset sum to zero
Algorithm:
- Set maxSum = 0 and sum = 0
- Iterate all element of array one by one
- sum += array[i]
- if maxSum < sum then
- Set maxSum = sum
- Else If sum < 0 then
- Set sum = 0
Latest Source Code:
Github: LargestContiguousSum.java
Output:
Array: [-2, -3, 4, -1, -2, 1, 5, -3] Sum: 7 Array: [2, -8, 3, -2, 4, -10] Sum: 5