[ create a new paste ] login | about

Link: http://codepad.org/wbXZY5zP    [ raw code | output | fork | 1 comment ]

C, pasted on Apr 10:
#include <stdio.h>

int main(void) {
int a[] = { -3, 100, -4, -2, 9, -63, -200, 55 };
int max_sum, temp_sum, i, n = 8, t;
temp_sum = max_sum = a[0];
for (i = 0; i < n; i++) {
    if (a[i] > 0)
        temp_sum += a[i];
    else {
        t = 0;
        while (a[i] < 0 && i < n) {
            t += a[i];
            i++;
        }
        if (temp_sum + t > 0) {
            temp_sum = temp_sum + t + a[i];
            if (temp_sum > max_sum)
                max_sum = temp_sum;
        } else if (i < n)
            temp_sum = a[i];
    }
}
if (temp_sum > max_sum)
    max_sum = temp_sum;
printf("Maximum Numbers is %d \n", max_sum);
return 0;
}


Output:
1
Maximum Numbers is 103 


Create a new paste based on this one


Comments:
posted by Govardhan on Oct 15
No it doesn't work properly.
If array is -3,100,-4,-2,1,-63,-200,55
Maximum sum is supposed to be 100, but this code is giving 95.
reply