Easy · Arrays

Max sum of subarray size K

Given an integer array nums and an integer k with 1 ≤ k ≤ nums.length, return the largest sum of any k consecutive elements.

Examples

Example 1

[2,1,5,1,3,2], k=3

Output: max sum = 9

Example 2

[1,9,-1,-2,7,3,-1,2], k=4

Output: max sum = 13

Rebuild it in the studio

Open every published interview problem, solution, scene, and memory room free, with no account.

More Arrays problems