Skip to content

Commit 66c75c6

Browse files
vishnu-mabranhe
authored andcommitted
Added Kadanes algorithm for finding Maximum Subarray Sum
1 parent 86120d3 commit 66c75c6

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy