Array Max Consecutive Sum

Description


Given array of integers, find the maximal possible sum of some of its k consecutive elements.

Example

For inputArray = [2, 3, 5, 1, 6] and k = 2, the output should be arrayMaxConsecutiveSum(inputArray, k) = 8. All possible sums of 2 consecutive elements are:

  • 2 + 3 = 5;
  • 3 + 5 = 8;
  • 5 + 1 = 6;
  • 1 + 6 = 7. Thus, the answer is 8.

Input/Output

  • [execution time limit] 4 seconds (js)

  • [input] array.integer inputArray

Array of positive integers.

Guaranteed constraints: 3 \leq inputArray.length \leq 10^5, 1 ≤ inputArray[i] ≤ 1000.

  • [input] integer k

An integer (not greater than the length of inputArray).

Guaranteed constraints: 1 ≤ k ≤ inputArray.length.

  • [output] integer

The maximal possible sum.

[JavaScript (ES6)] Syntax Tips

1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
    console.log("This prints to the console when you Run Tests");
    return "Hello, " + name;
}

Solution


1
2
3
4
5
6
7
8
9
10
11
function arrayMaxConsecutiveSum(inputArray, k) {

    var max = inputArray.slice(0,k).reduce((a,b)=>a+b);
    var cur = max;
    for(var i = k; i < inputArray.length; i++) {
        cur = cur + inputArray[i] - inputArray[i-k];
        if(cur>max)
            max = cur
    }
    return max
}