Created
September 4, 2022 19:48
-
-
Save codecakes/f9feeb4322f3c69d0587d7f945bc121a to your computer and use it in GitHub Desktop.
Largest Contiguous Sum of Array with positive and negative integers
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
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