1
1
"""
2
2
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
+
4
19
"""
5
20
def negative_exist (arr ):
6
21
7
22
"""
8
- checks wether the max value in -ve or not
23
+ checks whether the max value in -ve or not
9
24
eg :-
10
25
arr = [-2,-8,-9]
11
26
so max element is negative i.e. -2
@@ -40,9 +55,9 @@ def kadanes_implementation(arr):
40
55
and when value of max_till_sum is less than 0 it will assign 0 to i
41
56
and after whole process it will return the value
42
57
43
- So th output for above arr is :- 8
58
+ So the output for above arr is :- 8
44
59
"""
45
- if ( negative_exist (arr )< 0 ) :
60
+ if negative_exist (arr ) < 0 :
46
61
return negative_exist (arr )
47
62
48
63
max_sum = 0
@@ -62,8 +77,8 @@ def kadanes_implementation(arr):
62
77
if __name__ == "__main__" :
63
78
try :
64
79
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 ))
67
82
except ValueError :
68
83
print ("Please,enter decimal values" )
69
-
84
+
0 commit comments