Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
Last active August 29, 2015 14:04
Show Gist options
  • Save kartikkukreja/e3438cab495e4187d248 to your computer and use it in GitHub Desktop.
Save kartikkukreja/e3438cab495e4187d248 to your computer and use it in GitHub Desktop.
Maximum Subarray Problem
let A be the given array of integers
let maxSum = -infinity, maxLeft = 0, maxRight = 0, currentMax = 0, left = 0, right = 0
for i = 0 to A.length - 1
currentMax += A[i]
if currentMax > maxSum
maxSum = currentMax
right = i;
maxLeft = left
maxRight = right
if currentMax < 0
currentMax = 0
left = i + 1
right = i + 1
A[maxLeft...maxRight] is the maximum sum contiguous subarray.
return maxSum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment