-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_subarray_sum.py
More file actions
38 lines (32 loc) · 847 Bytes
/
Copy pathmax_subarray_sum.py
File metadata and controls
38 lines (32 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Max Sum Contiguous Subarray
# Write an efficient program to find the sum of contiguous subarray
# a one-dimensional array of numbers which has the largest sum.
def is_all_negetive(a):
for i in range(len(a)):
if a[i] > 0 :
return False
return True
def find_max(a):
max=a[0];i=0
while(i<len(a)):
if a[i] > max:
max=a[i]
i=i+1
return max
def max_subarray_sum(a):
if not is_all_negetive(a):
sum=a[0];i=1;max=a[0]
while(i<len(a)):
if sum + a[i] > 0:
sum = sum + a[i]
if max < sum:
max = sum
else:
sum=0
i=i+1
return max
else:
return find_max(a)
a=[ -163, -20 ]
# a=[-2]
print max_subarray_sum(a)