Algomination
Data Structures
AboutContact

Algomination

Learn algorithms and data structures through smooth, interactive visualizations.

SortingSearchingArray AlgorithmsData StructuresAboutContact

© 2026Algomination. Created by Omang Rawat & Rahul Soni.

Omang Rawat
Rahul Soni
All array algorithms

Sliding Window (Max Sum of K)

Time O(n)Space O(1)

Finds the maximum sum of any K consecutive elements. Instead of re-adding the whole window each time, it slides one step — subtracting the element that leaves and adding the one that enters.

start,end
2
1
5
1
3
2
8
1
Build the first window of size 3: add 2. Window sum = 2.
Step 1 / 17

About Sliding Window (Max Sum of K)

Finds the maximum sum of any K consecutive elements. Instead of re-adding the whole window each time, it slides one step — subtracting the element that leaves and adding the one that enters.

Time complexity: O(n). Space complexity: O(1).

How it works, step by step

Core idea — Sum the first K elements once, then reuse that sum: each slide drops the leftmost element and adds the next one on the right.

What each pass accomplishes — Every slide costs just one subtraction and one addition to get the new window's sum, which is compared against the best so far. The window visits each element once, so the whole array is scanned in O(n) rather than O(n·K).

Use the interactive visualizer above to run Sliding Window (Max Sum of K) on your own input and watch every comparison, swap, and operation animate step by step — pause, scrub, or replay at any speed.