1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

int MaxSubArray(int A[], int n) 
{
    int sum = A[0];
    int cur = A[0];
    for (int i = 1; i < n; ++i)
    {
        if (cur <0)
            cur = A[i];
		else
			cur += A[i];

        if (sum < cur) sum = cur;
    }

    return sum;
}



#include <stdio.h>

int main(int argc, char** argv)
{
    int a[]={-2,1,-3,4,-1,2,1,-5,4};
    int n = sizeof(a) / sizeof(int);

    printf("%d\n", MaxSubArray(a,n));
    return 0;
}
View Program Text


Test Status