beginner · sliding window · arrays
The busiest stretch of k hours
Problem
Visitor counts are recorded hourly. For a window length k, find the largest total number of visitors in any k consecutive hours.
Constraints
- 1 ≤ k ≤ n ≤ 32
- counts from −999 to 999 (negative values are allowed to keep the method general)
Examples
counts = [4, 2, 9, 1, 7, 3], k = 3 → 17 — 9 + 1 + 7.
Show the solution
Sliding window sum
Keep the sum of the current window; moving it one step changes it by (entering − leaving).
Why it is correct
After the first loop, window = sum of a[0..k). Each slide keeps window equal to the sum of a[i − k + 1..i]; best takes the maximum over all n − k + 1 windows.
Complexity
Time O(n), extra space O(1). k additions for the first window, then 2 operations for each of the n − k slides.
For contrast: Sum every window separately. That costs O(n · k) time and O(1) space.
Edge cases
- k = n: one window.
- k = 1: the maximum element.
Common mistakes
- Off-by-one when subtracting the leaving element (it is a[i − k]).
- Starting best at 0 when sums can be negative.
Step through it
The solution in four languages
Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.
namespace Bekadan.Dsa.Examples.Problems;
public static class FixedWindowMaxSum
{
/// <summary>The largest sum of k consecutive values (1 <= k <= n). O(n), O(1) space.</summary>
public static int Best(int[] counts, int k)
{
int window = 0;
for (int i = 0; i < k; i++)
window += counts[i];
int best = window;
for (int i = k; i < counts.Length; i++)
{
window += counts[i] - counts[i - k]; // one hour enters, one leaves
best = Math.Max(best, window);
}
return best;
}
}
Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/FixedWindowMaxSum.cs
from collections.abc import Sequence
def best(counts: Sequence[int], k: int) -> int:
"""The largest sum of k consecutive values (1 <= k <= n). O(n), O(1) space."""
window = 0
for i in range(k):
window += counts[i]
result = window
for i in range(k, len(counts)):
window += counts[i] - counts[i - k] # one hour enters, one leaves
result = max(result, window)
return result
Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/fixed_window_max_sum.py
package dev.bekadan.dsa.problems;
public final class FixedWindowMaxSum {
private FixedWindowMaxSum() {
}
/** The largest sum of k consecutive values (1 <= k <= n). O(n), O(1) space. */
public static int best(int[] counts, int k) {
int window = 0;
for (int i = 0; i < k; i++) {
window += counts[i];
}
int best = window;
for (int i = k; i < counts.length; i++) {
window += counts[i] - counts[i - k]; // one hour enters, one leaves
best = Math.max(best, window);
}
return best;
}
}
Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/FixedWindowMaxSum.java
#pragma once
#include <algorithm>
#include <cstddef>
#include <span>
namespace bekadan::dsa::problems {
// The largest sum of k consecutive values. Precondition: 1 <= k <= counts.size(). O(n), O(1) space.
inline int fixed_window_max_sum(std::span<const int> counts, std::size_t k) {
int window = 0;
for (std::size_t i = 0; i < k; ++i)
window += counts[i];
int best = window;
for (std::size_t i = k; i < counts.size(); ++i) {
window += counts[i] - counts[i - k]; // one hour enters, one leaves
best = std::max(best, window);
}
return best;
}
} // namespace bekadan::dsa::problems
Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/fixed_window_max_sum.hpp