Maximum Sum Subarray Size K
In this problem, you are looking at windows of exactly k numbers. A window is just one continuous chunk of the list. Your job is to check every possible chunk of that fixed size and return the largest sum you can find.
The important rule is that the size never changes. If k = 3, then every group you check must contain exactly 3 numbers. You are not allowed to use 2 numbers or 4 numbers instead. You slide that window from left to right and compare the totals.
For example, in [2,1,5,1,3,2] with k = 3, the windows are [2,1,5], [1,5,1], [5,1,3], and [1,3,2]. Their sums are 8, 7, 9, and 6, so the answer is 9. In [2,3,4,1,5] with k = 2, the best total is 7. If k is too large for the list, or not a valid size, the answer should be 0.
The final result is just the best sum from any window of size k.
Example Input & Output
Window sums are 8, 7, 9, 6 so the maximum is 9.
Window sums are 5, 7, 5, 6 so the maximum is 7.
No valid window of size 4 exists.
Algorithm Flow

Solution Approach
This is one of the most standard fixed-size sliding window problems. The window length is always exactly k, so the main optimization is to reuse the sum from the previous window instead of recomputing every window total from scratch.
The first thing to handle is invalid input. If k <= 0 or k > len(nums), then no valid window exists, so the answer should be 0. Once the size is valid, compute the sum of the first k numbers. That gives both the initial window sum and the first candidate for the maximum.
Then slide the window across the array. Every move changes the sum in a very predictable way: one number leaves from the left side, and one new number enters from the right side. So we update the running sum like this:
window_sum += nums[right] - nums[left]
After each update, compare the new window sum with the best answer seen so far and keep the larger one.
This avoids the waste of summing each length-k window from scratch. A brute-force solution would take O(nk) time because every window would cost up to k additions. The sliding-window version only does constant work per shift after the first window, so the full runtime becomes O(n).
This approach also handles negative values correctly. Even if some numbers are negative, the window rule does not change: remove the value that leaves and add the value that enters. We are still comparing fixed-size totals, so the same update logic works.
It is also worth noticing that the answer is about the largest sum, not the window itself. So we only need to remember the best numeric value unless the problem explicitly asks us to return the subarray too.
The extra space is O(1) because we only store a few counters. The key idea is that neighboring fixed-length windows overlap almost completely, so most of the old sum can be reused instead of recalculated.
Best Answers
class Solution {
public int maximum_sum_subarray_size_k(int[] nums, int k) {
int n = nums.length;
if (k <= 0 || k > n) return 0;
int window = 0;
for (int i = 0; i < k; i++) window += nums[i];
int best = window;
for (int i = k; i < n; i++) {
window += nums[i] - nums[i-k];
if (window > best) best = window;
}
return best;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
