Maximum Sum Subarray of Size K

easy

Find the maximum sum of any contiguous subarray of size k

Maximum Sum Subarray of Size K

Key Insight

Fixed window of size k: slide by adding right element and removing left element

Step 1Build Initial Window
Window sum: 8k = 3
L
R
2
0
1
1
5
2
1
3
3
4
2
5
[———]window

Sum the first k elements to form the initial window.

1 / 5
Practice the Code

Step-by-Step Walkthrough: Maximum Sum Subarray of Size K

Fixed window of size k: slide by adding right element and removing left element

  1. Build Initial Window

    Sum the first k elements to form the initial window.

  2. Slide Window Right

    Add the next element and remove the leftmost to slide the window.

  3. Continue Sliding

    Slide again: add right element, remove left element.

  4. Last Window

    Slide to the final position and compare.

  5. Maximum Found

    The maximum sum window is [5, 1, 3] with sum 9.