Skip to content

Commit 6d7030d

Browse files
authored
max subarray sum added
1 parent bb41d7e commit 6d7030d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

maxsubarr.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Python program to find maximum contiguous subarray
2+
3+
# Function to find the maximum contiguous subarray
4+
from sys import maxint
5+
def maxSubArraySum(a,size):
6+
7+
max_so_far = -maxint - 1
8+
max_ending_here = 0
9+
10+
for i in range(0, size):
11+
max_ending_here = max_ending_here + a[i]
12+
if (max_so_far < max_ending_here):
13+
max_so_far = max_ending_here
14+
15+
if max_ending_here < 0:
16+
max_ending_here = 0
17+
return max_so_far
18+
19+
# Driver function to check the above function
20+
a = [-13, -3, -25, -20, -3, -16, -23, -12, -5, -22, -15, -4, -7]
21+
print "Maximum contiguous sum is", maxSubArraySum(a,len(a))

0 commit comments

Comments
 (0)