An interactive step-by-step visualisation of the Sliding Window pattern. Watch how the window slides through the array, efficiently updating the sum in O(1) per step.
1def max_sum_subarray(nums: list[int], k: int) -> int:2 window_sum = sum(nums[:k])3 max_sum = window_sum45 for i in range(k, len(nums)):6 window_sum += nums[i] - nums[i - k]7 max_sum = max(max_sum, window_sum)89 return max_sum
We have an array [2, 1, 5, 1, 3, 2] and need to find the maximum sum of any 3 consecutive elements.