Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Maximum Sum Subarray of Size K

Sliding Windoweasy

Implement `maxSumK(nums, k)` returning the largest sum of any contiguous subarray of length `k`.

Sample tests

Input: maxSumK([2,1,5,1,3,2], 3)
Output: 9
Input: maxSumK([2,3,4,1,5], 2)
Output: 7

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Recomputing each window from scratch makes it O(n*k).

Learning resources

  • MDN: String
Approach & explanation (try first)

A fixed sliding window updates the sum in O(1) per step for O(n) total.

Loading...
⌘/Ctrl + Enter

Run your code to see results.