Skip to content

Instantly share code, notes, and snippets.

@prasadwrites
Last active August 15, 2023 23:46
Show Gist options
  • Select an option

  • Save prasadwrites/9d582ed7b2e0a90bb1f3 to your computer and use it in GitHub Desktop.

Select an option

Save prasadwrites/9d582ed7b2e0a90bb1f3 to your computer and use it in GitHub Desktop.
Kadane's Algorithm for finding Maximum Subarrary
#! /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