Given an array of natural numbers, find the sum of the largest contiguous subarray of a given size k. Example:
int array = {3, 6, 90, 23, 7, 1, 8, 3};
int k = 3;
The sum should be 120 (90+23+7).
Example code:
#include <stdio.h>
int main(void) {
int arraysize = 9;
int array[9] = {3, 6, 9, 20, 40, 60, 1, 2, 3};
int k = 3;
int max = 0;
int maxcurrent = 0;
for (int i = 0; i < (arraysize - k); i++) {
for (int j = 0; j < k; j++) {
maxcurrent += array[i + j];
}
if (maxcurrent > max) { max = maxcurrent; }
maxcurrent = 0;
}
printf("%d\n", max);
}