Skip to content

Commit

Permalink
Merge pull request #936 from ARYASINGHBJC/patch-1
Browse files Browse the repository at this point in the history
Improved readability
  • Loading branch information
smv1999 authored Oct 5, 2022
2 parents 81a11e1 + 8e3997e commit 8b7e017
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions Arrays/Kadane's_Algorithm.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
#Kadane's Algorithm

def kadaneAlgo(n,arr):
sum_num = 0
max_sum = 0
for i in range(n):
sum_num += arr[i]; #maximum ending here
if arr[i] > sum_num:
sum_num = arr[i]
if max_sum < sum_num: #comparing maximum ending here , maximum so far
max_sum = sum_num
def kadaneAlgo(arr):
maxSumSoFar = arr[0]
max_sum = arr[0]
for ele in arr[1:]:
maxSumSoFar = max(ele, ele + maxSumSoFar) #current maximum sum so far
max_sum = max(max_sum, maxSumSoFar) #maximum sum so far
return max_sum

n = int(input())
l = [int(x) for x in input().split()]
max_sm = kadaneAlgo(n,l)
l = list(map(int, input().split()))
max_sm = kadaneAlgo(l)
print(max_sm)

0 comments on commit 8b7e017

Please sign in to comment.