Largest Contiguous Sum in a array

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:
  1. Set maxSum = 0 and sum = 0
  2. Iterate all element of array one by one
    1. sum += array[i]
    2. if maxSum < sum then
      1. Set maxSum = sum
    3. Else If sum < 0 then
      1. 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
Author: Hrishikesh Mishra