From 81551626bb76c800290ad43753dd0fe6d9e6dbe6 Mon Sep 17 00:00:00 2001 From: DarkMoron <57228056+DarkMoron@users.noreply.github.com> Date: Fri, 2 Oct 2020 13:21:59 +0530 Subject: [PATCH] Adding Kadane's algorithm py --- delete_this/kadanes_algorithm.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 delete_this/kadanes_algorithm.py diff --git a/delete_this/kadanes_algorithm.py b/delete_this/kadanes_algorithm.py new file mode 100644 index 0000000..08ac4ae --- /dev/null +++ b/delete_this/kadanes_algorithm.py @@ -0,0 +1,21 @@ +# Python program to find maximum contiguous subarray using Kadane's Algorithm. + +# Function to find the maximum contiguous subarray +from sys import maxint +def maxSubArraySum(a,size): + + max_so_far = -maxint - 1 + max_ending_here = 0 + + for i in range(0, size): + max_ending_here = max_ending_here + a[i] + if (max_so_far < max_ending_here): + max_so_far = max_ending_here + + if max_ending_here < 0: + max_ending_here = 0 + return max_so_far + +# Driver function to check the above function +a = [-2, -3, 4, -1, -2, 1, 5, -3] +print "Maximum contiguous sum is", maxSubArraySum(a,len(a))