Maximum Sum Subarray of Size K

Given an integer array nums and an integer k, return the maximum sum of any contiguous subarray containing exactly k elements.

Given an integer array `nums` and an integer `k`, return the maximum sum of any contiguous subarray containing exactly `k` elements.

- `1 <= nums.length <= 10^5` - `-10^4 <= nums[i] <= 10^4` - `1 <= k <= nums.length` - The interactive visualizer accepts at most 12 values, and interactive values must be between -1,000 and 1,000; longer or wider valid arrays are execution-only.

Example 1: input nums = [2,1,5,1,3,2], k = 3; output 9. The size-3 subarray [5,1,3] has the largest sum, 9.

Example 2: input nums = [-4,-2,-7,-1], k = 2; output -6. Every window sum is negative; [-4,-2] is still the maximum at -6.

Category: Sliding Window. Topic: sliding-window. Difficulty: Easy. Patterns: sliding-window.