Trying to create a function where an array a is passed as an argument and what is returned is a pair of indices x,y such that the maximum sum is sum(a[x:y]).
For example, let's say I have the array [4, -2, -3, 7, 3, -1]
. The function would take in this array and spit out (3, 4) because the sequence of number from index 3 to index 4 is the biggest one you can make in this array. 10 is the largest number you'll find in this array from adding any sequence together.
This is the code I have so far, which more or less works, but it takes forever for arrays > 10000 in length. Any suggestions?
def bentley(a):
max = 0
indices = 0,0
for x in range(len(a)):
for y in range(len(a)):
if sum(a[x:y]) > max:
max = sum(a[x:y])
indices = x,y
return indices