int rstart = 0;
int rend = 0;
int maxsum = 0;
for(int start = 0; start < size; ++start){
for(int end = start; end < size; ++end){
int sum = 0;
for(int ct = start; ct <= end; ++ct)
sum += data[ct];
if(sum > maxsum){
rstart = start;
rend = end;
maxsum = sum;
}
}
}
int rstart = 0;
int rend = 0;
int maxsum = 0;
for(int start = 0; start < size; ++start){
int sum = 0;
for(int end = start; end < size; ++end){
sum += data[end];
if(sum > maxsum){
rstart = start;
rend = end;
maxsum = sum;
}
}
}
sum (data, (switch(data, sizeof(int))
sum (data, (switch(data, sizeof(int))
Using HLA/FASM.
Actually the first test is < 0 rather than < 1. I don't believe Sharmair's correctly handles the array Runter posted.
This - > {10,-10,9,2}
Yes, I was aware of how it worked with leading zero sum ranges (they don't contribute to the
maxsum and are not included). I had 3 different ways that line could work based on how you
wanted to handle that, and if your were using floating point numbers. I also have thought out
changes to other lines to handle trailing zero sum ranges or subsequent equal max sum ranges.
And even code to handle the no positive number case.
The OP only specified the max sum, and did not even include range limits in either of his solutions.
Let alone specified what one of mutiple equal max sum ranges was more 'correct' then another.