In this challenge, given an array of integers, the goal is to efficiently find the subarray that has the greatest value when all of its elements are summed together. Note that because some elements of the array may be negative, the problem is not solved by simply picking the start and end elements of the array to be the subarrray, and summing the entire array.
is the right way to do it. is there any other way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int max_sum(int *vector, int len)
{
int best = 0, current = 0;
int i = 0;
for(i = 0; i < len; ++i)
{
current += *(vector + i);
if(current < 0)
{
current = 0;
}
best = best > current ? best : current;
}
return best;
}