Skip to content

Commit 681bdf9

Browse files
authored
Merge pull request ashutosh97#92 from arc9693/kandaneImplementationinPython
Added python sol of kadane's algo
2 parents 92872d6 + d94754b commit 681bdf9

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Kadane's Algorithm/kadane.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def maxSubarraySum(a, l):
2+
_max = -999999
3+
max_here = 0
4+
for i in range(l):
5+
max_here = max_here + a[i]
6+
if (_max < max_here):
7+
_max = max_here
8+
if (max_here < 0):
9+
max_here = 0
10+
11+
12+
return _max
13+
14+
15+
if __name__ == '__main__':
16+
arr=[-2, -3, 4, -1, -2, 1, 5, -3]
17+
l=len(arr)
18+
m = maxSubarraySum(arr, l)
19+
print(m," is the max subarray sum")
20+
21+

0 commit comments

Comments
 (0)