Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding kadane's algorithm in Python #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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))