forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargest_subarray_sum.py
More file actions
Latest commit
23 lines (19 loc) · 678 Bytes
/
Copy pathlargest_subarray_sum.py
File metadata and controls
23 lines (19 loc) · 678 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
fromsysimportmaxsize
defmax_sub_array_sum(a: list, size: int=0):
"""
>>> max_sub_array_sum([-13, -3, -25, -20, -3, -16, -23, -12, -5, -22, -15, -4, -7])
-3
"""
size=sizeorlen(a)
max_so_far=-maxsize-1
max_ending_here=0
foriinrange(0, size):
max_ending_here=max_ending_here+a[i]
ifmax_so_far<max_ending_here:
max_so_far=max_ending_here
ifmax_ending_here<0:
max_ending_here=0
returnmax_so_far
if__name__=="__main__":
a= [-13, -3, -25, -20, 1, -16, -23, -12, -5, -22, -15, -4, -7]
print(("Maximum contiguous sum is", max_sub_array_sum(a, len(a))))