Interview Questions/Coding/Maximum Sum Window

Maximum Sum Window

Preview mode. Log in to edit, run, submit, and save progress.

Easy

Maximum Sum Window

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

Examples

Example 1
Input:
nums = [1, 2, 3, 4, 5], k = 3
Output:
12

Explanation: The subarray [3, 4, 5] has the maximum sum.

Approach hint

Start with the simplest clear approach, explain the trade-off, then move toward the cleaner answer.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp