Skip to content

Commit 56e1222

Browse files
authored
Update Kadane's_algorithm.py
1 parent 23e85f6 commit 56e1222

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

maths/Kadane's_algorithm.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
"""
22
Kadane's algorithm to get maximum subarray sum
3-
Please enter the decimal values only and use spaces as seperator
3+
Please enter the decimal values only and use spaces as separator
4+
"""
5+
6+
7+
"""
8+
Doc-Test :-
9+
10+
Enter the element of array with spaces :-
11+
>>> 2 3 -9 8 -2
12+
Maximum subarray sum of [2, 3, -9, 8, -2] is :- 8
13+
14+
15+
Enter the element of array with spaces :-
16+
>>> -9 -8 -7 -6 -2
17+
Maximum subarray sum of [-9, -8, -7, -6, -2] is :- -2
18+
419
"""
520
def negative_exist(arr):
621

722
"""
8-
checks wether the max value in -ve or not
23+
checks whether the max value in -ve or not
924
eg :-
1025
arr = [-2,-8,-9]
1126
so max element is negative i.e. -2
@@ -40,9 +55,9 @@ def kadanes_implementation(arr):
4055
and when value of max_till_sum is less than 0 it will assign 0 to i
4156
and after whole process it will return the value
4257
43-
So th output for above arr is :- 8
58+
So the output for above arr is :- 8
4459
"""
45-
if(negative_exist(arr)<0):
60+
if negative_exist(arr) < 0:
4661
return negative_exist(arr)
4762

4863
max_sum = 0
@@ -62,8 +77,8 @@ def kadanes_implementation(arr):
6277
if __name__ == "__main__":
6378
try:
6479
print("Enter the element of array with spaces :- ")
65-
arr = list(map(int, input().split()))
66-
print(kadanes_implementation(arr))
80+
arr = [int(x) for x in input().split()]
81+
print("Maximum subarray sum of ",arr,"is :-",kadanes_implementation(arr))
6782
except ValueError:
6883
print("Please,enter decimal values")
69-
84+

0 commit comments

Comments
 (0)