Last active
August 15, 2023 23:46
-
-
Save prasadwrites/9d582ed7b2e0a90bb1f3 to your computer and use it in GitHub Desktop.
Kadane's Algorithm for finding Maximum Subarrary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/env/path python | |
| class Solution: | |
| ##Complete this function | |
| #Function to find the sum of contiguous subarray with maximum sum. | |
| def maxSubArraySum(self,arr,N): | |
| ##Your code here | |
| if (N < 1) or (N > 1000000): | |
| print("error : invalid array size") | |
| return | |
| max_till_now = arr[0] | |
| max_ending = 0 | |
| for i in range(len(arr)): | |
| max_ending = max_ending + arr[i] | |
| if max_ending < 0: | |
| if(max_ending > max_till_now): | |
| max_till_now = max_ending | |
| max_ending = 0 | |
| elif (max_till_now < max_ending): | |
| max_till_now = max_ending | |
| return max_till_now |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment