← Pointer Manipulation

Micro-Drill #89 — Fixed window sum

Pointer Manipulation Target: 10s

Fixed-window sliding avoids re-summing by adding the new element and removing the old one.

window = sum(a[:k])
best = window
for i in range(k, len(a)):
    window += a[i] - a[i - k]
    best = max(best, window)

Type it from memory. Go.

Practice Problems

Related Coding Drills

← Micro #88 Micro #90 →