From 8e3997ec297f5c4c0747b413879adb3acf646406 Mon Sep 17 00:00:00 2001 From: Arya Singh <72375247+ARYASINGHBJC@users.noreply.github.com> Date: Mon, 19 Sep 2022 18:53:51 +0530 Subject: [PATCH] Improved readability --- Arrays/Kadane's_Algorithm.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/Arrays/Kadane's_Algorithm.py b/Arrays/Kadane's_Algorithm.py index 903a1dc93..89eefa8ac 100644 --- a/Arrays/Kadane's_Algorithm.py +++ b/Arrays/Kadane's_Algorithm.py @@ -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)