#include #include int largest_k_subsequence(int * items, int k, int N) { if (k == 0) return 0; int largest, i; int current = 0; for(i = 0; i < k; i++) current += items[i]; largest = current; for(i = k; i < N; i++) { current = current - items[i-k] + items[i]; if (current > largest) largest = current; } return largest; } int largest_subsequence(int * items, int N) { if (N == 0) return 0; int max_so_far = items[0]; int max_ending_here = items[0]; int i; for(i = 1; i < N; i++) { if(max_ending_here <= 0) max_ending_here = items[i]; else max_ending_here += items[i]; if(max_ending_here >= max_so_far ) max_so_far = max_ending_here; } return max_so_far ; } int main() { int A[] = {4, -7, 5, 4, 3, -10, 8}; int k; for (k = 0; k <= 7; k++) { int result = largest_k_subsequence(A, k, 7); printf("k = %d, result = %d\n", k, result); } int result = largest_subsequence(A, 7); printf("\nlargest_subsequence(A, 7) = %d\n", result); return 0; }