int a[ ] = { 6, -8, 3, -2, 4};
maxsum = 0;
sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (maxsum < sum) {
maxsum = sum;
} else if (sum < 0)
{
sum = 0;
}
}

Advertisements