Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lists/kadane's_algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Python program to find maximum contiguous subarray

def maxSubArraySum(a,size):

max_so_far = 0
max_ending_here = 0

for i in range(0, size):
max_ending_here = max_ending_here + a[i]
if max_ending_here < 0:
max_ending_here = 0

# Do not compare for all elements. Compare only
# when max_ending_here > 0
elif (max_so_far < max_ending_here):
max_so_far = max_ending_here

return max_so_far


# Driver function to check the above function (with a sample test case)

a = [3, 3, -25, 20, -3, 16, 23, -12, -5, 22, -15, 4, -7]
print "Maximum contiguous sum is", maxSubArraySum(a,len(a))