Last active
August 29, 2015 14:04
-
-
Save kartikkukreja/e3438cab495e4187d248 to your computer and use it in GitHub Desktop.
Maximum Subarray Problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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