Maximum Subarray Sum Codewars Solution

The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers:

maxSequence([-2, 1, -3, 4, -1, 2, 1, -5, 4])
// should be 6: [4, -1, 2, 1]

Easy case is when the list is made up of only positive numbers and the maximum sum is the sum of the whole array. If the list is made up of only negative numbers, return 0 instead.

Empty list is considered to have zero greatest sum. Note that the empty list or array is also a valid sublist/subarray.



procedure maxSequence(arr)
1. set maxSeq and currSeq to 0 // since we should return only positive numbers or 0
2. for each element in arr
   A. if 0 < element + currSeq: then currSeq = element + currSeq
   B. if currSeq > maxSeq: then maxSeq = currSeq
3. return maxSeq

JavaScript Implementation

Dropping the constatns the algorithm runs at linear timeO(n).


Leave a comment

Your email address will not be published. Required fields are marked *