From b48bcbae7a5bd2d72d53c5c2205e7f8dbc4bac82 Mon Sep 17 00:00:00 2001 From: SomRawat <76642632+SomRawat@users.noreply.github.com> Date: Fri, 15 Oct 2021 11:30:41 +0530 Subject: [PATCH] Kadane's Algorithm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Kadane’s algorithm is one of the famous approaches to solve the problem using dynamic programming The time complexity of kadane’s algorithm for an array containing n integer element is O(n) as only one for loop is to be executed throughout the program. Similarly, the auxiliary space complexity of the algorithm is O(1). --- Kadanes_Algorithm.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Kadanes_Algorithm.py diff --git a/Kadanes_Algorithm.py b/Kadanes_Algorithm.py new file mode 100644 index 0000000..06b8f9e --- /dev/null +++ b/Kadanes_Algorithm.py @@ -0,0 +1,18 @@ +def maxSubArraySum(arr,size): + + max_till_now = arr[0] + max_ending = 0 + + for i in range(0, size): + max_ending = max_ending + arr[i] + if max_ending < 0: + max_ending = 0 + + + elif (max_till_now < max_ending): + max_till_now = max_ending + + return max_till_now + +arr = [-2, -3, 4, -1, -2, 5, -3] +print("Maximum Sub Array Sum Is" , maxSubArraySum(arr,len(arr)))