Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8e3997e

Browse files
Improved readability
1 parent 81a11e1 commit 8e3997e

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

‎Arrays/Kadane's_Algorithm.py‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
#Kadane's Algorithm
22

3-
def kadaneAlgo(n,arr):
4-
sum_num = 0
5-
max_sum = 0
6-
for i in range(n):
7-
sum_num += arr[i]; #maximum ending here
8-
if arr[i] > sum_num:
9-
sum_num = arr[i]
10-
if max_sum < sum_num: #comparing maximum ending here , maximum so far
11-
max_sum = sum_num
3+
def kadaneAlgo(arr):
4+
maxSumSoFar = arr[0]
5+
max_sum = arr[0]
6+
for ele in arr[1:]:
7+
maxSumSoFar = max(ele, ele + maxSumSoFar) #current maximum sum so far
8+
max_sum = max(max_sum, maxSumSoFar) #maximum sum so far
129
return max_sum
1310

14-
n = int(input())
15-
l = [int(x) for x in input().split()]
16-
max_sm = kadaneAlgo(n,l)
11+
l = list(map(int, input().split()))
12+
max_sm = kadaneAlgo(l)
1713
print(max_sm)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /