8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 86120d3 commit 66c75c6Copy full SHA for 66c75c6
algorithms/dynamic-programming/kadanes_algorithm.py
@@ -0,0 +1,27 @@
1
+def Kadane(array):
2
+ partialSum = bestSum = array[0]
3
+ fromIndex = toIndex = 0
4
+
5
+ for i in range(1, len(array)):
6
+ if array[i] > partialSum + array[i]:
7
+ partialSum = array[i]
8
+ fromIndex = i
9
+ else:
10
+ partialSum += array[i]
11
12
+ if partialSum >= bestSum:
13
+ bestSum = partialSum
14
+ toIndex = i
15
16
+ return {
17
+ "fromIndex" : fromIndex,
18
+ "toIndex" : toIndex,
19
+ "bestSum" : bestSum
20
+ }
21
22
+n = int(input("Enter the size of the array: "))
23
+print("Input the array")
24
+array = map(int,raw_input().split())
25
26
+kadane = Kadane(array)
27
+print("Sum: %d From: %d To: %d" % (kadane['bestSum'], kadane['fromIndex'], kadane['toIndex']))
0 commit comments