Skip to content
Onsite

Maximum Sum Subarray of Size K

EasyP2Coding

Given an integer array nums and an integer k, return the largest sum of any contiguous subarray of length exactly k.

Example 1

Input
nums = [2,1,5,1,3,2], k = 3
Output
9

Example 2

Input
nums = [2,3,4,1,5], k = 2
Output
7

Example 3

Input
nums = [5], k = 1
Output
5

Constraints

  • 1 <= k <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4

Hints

Run checks the 3 visible tests, Submit all 11.