Skip to content

Sliding Window Max Sum of K
Step by Step

Every move adds the number that enters the window and subtracts the number that leaves.

Move the window back and forth, then share a link to the step where the total looked wrong.

What You Watch While the Window Slides

The window and its live total change on every step.

Every move changes only two numbers in the total.

CodeStory's visualiser shows the window growing and shrinking with the live total inside it. Highlighted code lines follow along, and you can step back, step forward or play.

Start with a small k. Then open Wide window to compare brute force with the sliding version.

How the Window Slides Along the Array

Steps below follow the window on your screen.

  1. 1Add up the first k numbers to get the first window sum.
  2. 2Add the next number, which enters on the right.
  3. 3Subtract the number that leaves on the left.
  4. 4Keep the larger of the new sum and the top sum so far.
  5. 5Stop when the window reaches the last number.

Where Brute Force Goes Slow

The switch lives in the visualiser. Flip it on and watch the additions grow.

  • Brute forceAdds all k numbers again for every window, so you get about n × k additions instead of about 2n. Load Wide window to see the gap.

Window Inputs Worth Trying

Six presets cover the edge cases that break sliding windows.

  • k = 1Every window is one number, so the answer is the maximum. Sliding saves nothing here.
  • k = nOnly one window fits, so the window never slides.
  • All negativeEvery sum is below 0, so the top sum starts from the first window and never from 0.
  • k > nNo window fits at all.
  • Every window tiesOnly a bigger sum replaces the top sum, so the first window stays.
  • Wide windowSwitch on brute force here too and watch it add 6 numbers again for every window.

Sliding Window Cost in Time and Space

Interviewers ask why sliding beats brute force, and the visualiser counts real operations.

Time
O(n)counted from the additions and subtractions you step through
Space
O(1)extra memory is a few variables
Pattern
Sliding windowAdd the new number and drop the old one.

Everything in the App in One Place

Practice, interviews and tools all start free. Swipe to see the rest.

Sign up for 100 AI credits now and get 100 more every month. Start free in the app

Sliding Window Visualiser FAQs

Open the visualiser and follow the window step by step.

Open the visualiser
What does the sliding window visualiser show?

CodeStory's visualiser shows the window growing and shrinking with its live total, plus the highlighted line of code beside it. You can move one step at a time in either direction or play it.

What is the time complexity of max sum of k in a row?

O(n) time and O(1) space. CodeStory's visualiser counts real operations while it plays, so you watch the additions stay near 2n instead of trusting a formula.

What is the sliding window technique?

Sliding window keeps a range of the array and moves its edges instead of starting over. Here the window holds k numbers, so every move adds one number and subtracts one.

Why is brute force slower than a sliding window?

Brute force adds all k numbers again for every window, which costs about n × k additions. Sliding adds one number and subtracts one, so the cost stays near 2n.

Can I use my own input?

Yes. Enter your own array and a value for k, and the visualiser slides the window across it.

Is the visualiser free?

Yes. You get 100 AI credits at sign up and 100 more every month, and watching an algorithm spends none.

Stuck on Sliding Window? Get a Free Callback

Tell CodeStory which window problem loses you, and a mentor plans what comes next.

  1. 1CodeStory checks your level, from window basics to interview ready.
  2. 2CodeStory explains online and campus batches, with timings that fit your week.
  3. 3You decide. No pressure, no payment on the call.

Get a free callback

I am a
I want to learn

Your number is only used for a CodeStory callback.