Skip to content

Commit 1037948

Browse files
committed
added kadane's algorithm
1 parent 43a0eb7 commit 1037948

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"""
2+
What is Kadane's Algorithm?
3+
Kadane's Algorithm is a way to find the maximum subarray sum in an array with a runtime of O(n).
4+
It is a dynamic programming algorithm that uses the fact that the maximum subarray sum ending at index i is either the value at index i or the maximum subarray sum ending at index i-1 plus the value at index i.
5+
6+
Note: The subarray must be contiguous, all the values in the subarray must be next to each other in the original array.
7+
8+
How does it work?
9+
The algorithm works by iterating through the array and keeping track of the maximum subarray sum seen so far and the maximum subarray sum ending at the current index.
10+
The maximum subarray sum ending at the current index is either the value at the current index or the maximum subarray sum ending at the previous index plus the value at the current index.
11+
12+
Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3}
13+
max_so_far = INT_MIN
14+
max_ending_here = 0
15+
16+
for i=0, a[0] = -2
17+
max_ending_here = max_ending_here + (-2)
18+
Set max_ending_here = 0 because max_ending_here < 0
19+
and set max_so_far = -2
20+
21+
for i=1, a[1] = -3
22+
max_ending_here = max_ending_here + (-3)
23+
Since max_ending_here = -3 and max_so_far = -2, max_so_far will remain -2
24+
Set max_ending_here = 0 because max_ending_here < 0
25+
26+
for i=2, a[2] = 4
27+
max_ending_here = max_ending_here + (4)
28+
max_ending_here = 4
29+
max_so_far is updated to 4 because max_ending_here greater
30+
than max_so_far which was -2 till now
31+
32+
for i=3, a[3] = -1
33+
max_ending_here = max_ending_here + (-1)
34+
max_ending_here = 3
35+
36+
for i=4, a[4] = -2
37+
max_ending_here = max_ending_here + (-2)
38+
max_ending_here = 1
39+
40+
for i=5, a[5] = 1
41+
max_ending_here = max_ending_here + (1)
42+
max_ending_here = 2
43+
44+
for i=6, a[6] = 5
45+
max_ending_here = max_ending_here + (5)
46+
max_ending_here = 7
47+
max_so_far is updated to 7 because max_ending_here is
48+
greater than max_so_far
49+
50+
for i=7, a[7] = -3
51+
max_ending_here = max_ending_here + (-3)
52+
max_ending_here = 4
53+
54+
Time Complexity: O(n)
55+
Space Complexity: O(1)
56+
"""
57+
58+
from sys import maxint
59+
60+
# maxint is a constant that holds the maximum possible value for an integer in Python.
61+
62+
63+
def maxSubArraySum(a, size):
64+
# we take the max_so_far to be the smallest possible integer value
65+
max_so_far = -maxint - 1
66+
67+
# initialize max_ending_here to 0
68+
max_ending_here = 0
69+
70+
for i in range(0, size):
71+
max_ending_here = max_ending_here + a[i]
72+
if max_so_far < max_ending_here:
73+
max_so_far = max_ending_here
74+
75+
# if max_ending_here is negative, we set it to 0
76+
if max_ending_here < 0:
77+
max_ending_here = 0
78+
79+
return max_so_far
80+
81+
82+
# Driver function to check the above function
83+
84+
85+
a = [-2, -3, 4, -1, -2, 1, 5, -3]
86+
87+
print("Maximum contiguous sum is", maxSubArraySum(a, len(a)))

0 commit comments

Comments
 (0)