Skip to content

Instantly share code, notes, and snippets.

@codecakes
Created September 4, 2022 19:48
Show Gist options
  • Save codecakes/f9feeb4322f3c69d0587d7f945bc121a to your computer and use it in GitHub Desktop.
Save codecakes/f9feeb4322f3c69d0587d7f945bc121a to your computer and use it in GitHub Desktop.
Largest Contiguous Sum of Array with positive and negative integers
def largest_array_sum(arr):
n = len(arr)
if n < 1:
return 0
if n < 2:
return arr[0]
max_sum = float("-inf")
current_sum = arr[0]
for num in arr[1:]:
current_sum = max(current_sum + num, num)
max_sum = max(max_sum, current_sum)
return max_sum
print(largest_array_sum([7, 1, 2, -1, 3, 4, 10, -12, 3, 21, -19]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment