Skip to content

Instantly share code, notes, and snippets.

@jeffcarp
Last active December 25, 2015 10:09
Show Gist options
  • Save jeffcarp/6959725 to your computer and use it in GitHub Desktop.
Save jeffcarp/6959725 to your computer and use it in GitHub Desktop.
Max Consecutive Sum (problem of the week on http://codingforinterviews.com)
(defun max_consecutive_sum (arr)
(let ((max_ending_here 0) (max_so_far 0))
(dotimes (x (length arr))
(setf max_ending_here (max 0 (+ (aref arr x) max_ending_here)))
(setf max_so_far (max max_so_far max_ending_here)))
max_so_far))
(format t "~S~%" (max_consecutive_sum #(-1 5 6 -2 20 -50 4))) ; 29
(format t "~S~%" (max_consecutive_sum #(-2 1 -3 4 -1 2 1 -5 4))) ; 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment