Fixed Size Sliding Window Algorithm

Maximum Sum of Subarray of Size K

Problem Statement

Input: Array: [2, 2, 1, 5, 3, 0], Window size (K): 3

Goal: Find the maximum sum of any subarray that contains exactly 3 consecutive elements.

Step 1: Initial Window [2, 2, 1]

Current Sum: 5
Maximum Sum So Far: 5

Window Position

Index 0 to 2

Window Elements

[2, 2, 1]

Calculation

2 + 2 + 1 = 5

Step Description

Initialize the first window of size 3

🎉 Algorithm Complete!

The maximum sum of any subarray of size 3 is: 9

Found in window [1, 5, 3] at indices 2-4