Understanding Maximum Sum of Size K
Return the largest sum of any contiguous subarray containing exactly k values.
How it works
Reuse a fixed-size window sum by subtracting the departing value.
Accumulate incoming values. Remove the value that is now more than k positions behind. Compare complete windows, including all-negative windows.
Try an example
Input: nums = [2, 1, 5, 1, 3, 2], k = 3
Output: 9
Reuse a fixed-size window sum by subtracting the departing value.
Constraints
- 1–64 integers between -1,000,000 and 1,000,000.
- 1 ≤ k ≤ nums.length; negative values are allowed.
Use the simulator
Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.
Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.