COMPILED CANVAS
DSA IN JAVA

1 billion steps → 2 million

The sliding window trick

Tap to start →

IDEA

Don't start over

13246

Slide the window: add the new number, drop the old one.

1 of 4 · keep tapping →
FIXED

Size never changes

sum += a[i]; if (i >= k) sum -= a[i - k]; if (i >= k - 1) best = max(best, sum);

Biggest sum of exactly k numbers in a row.

2 of 4 · keep tapping →
VARIABLE

Grow, then shrink

shrink with if ✗
shrink with while ✓

Grow the right side. While the rule breaks, shrink the left.

3 of 4 · keep tapping →
WHY

O(n)

Restart each window
Sliding

Each number enters once and leaves once.

4 of 4 · keep tapping →
⚠ MOST PEOPLE MISS THIS

The mistake

Shrinking a variable window with if. It may need to shrink more than once: use while.

QUICK QUIZ

Can you get it?

Tap an answer. The full video explains why.

📸 SCREENSHOT THIS

Cheat sheet

  • Add new, drop old
  • Fixed: size k
  • Variable: grow then shrink
  • Shrink with while
  • O(n) time
COMPILED CANVAS

Want the full lesson?

▶ Watch the full videoPractice + free notes

Free DSA in Java course. Subscribe to Compiled Canvas on YouTube.

Watch the full video