first of all some synatax error in above code segment . right code given below
# include <stdio.h>
int main () {
int sum = 0, maxsum = 0, i, n = 6;
int a [] = {2, -2, -1, 3, 4, 2};
for (i = 0; i < n; i++) {
if (i == 0 || a [i] < 0 || a [i] < a [i - 1]) {
if (sum > maxsum) maxsum = sum;
sum = (a [i] > 0) ? a [i] : 0;
}
else
sum += a [i];
}
if (sum > maxsum) maxsum = sum ;
printf ("%d\n", maxsum);
}
ans is C .
simply execute and trace value of sum and maxsum for i=0 to 5
i --> initially 0 1 2 3 4 5
sum 0 0 2 0 3 7 2
maxsum 0 0 2 2 2 2 7
so maxsum = 7