Skip to content

Commit a0cbb1a

Browse files
Merge pull request souravjain540#148 from dinesh9-ai/main
added kadanes algorithm
2 parents 5466cd7 + f243464 commit a0cbb1a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

kadanes_algorithm.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
This algorithm is used to find the largest sum in the given array in O(n) Time complexity
3+
"""
4+
5+
arr=[-1,0,3,4,2,6,-10,5,-9]
6+
7+
max_sum=0
8+
curr_sum=0
9+
10+
for i in arr:
11+
curr_sum += i
12+
max_sum=max(max_sum,curr_sum)
13+
14+
if curr_sum<0:
15+
curr_sum=0
16+
17+
18+
print(max_sum)

0 commit comments

Comments
 (0)