easySliding WindowPure DSA~15 min
Peak Traffic Window
A capacity-planning dashboard reports the busiest k-minute stretch of the day. Given per-minute request counts, find the maximum total requests across any contiguous window of exactly k minutes.
Problem
Given an array counts of length n and an integer k (1 ≤ k ≤ n), return the maximum sum of any contiguous subarray of length exactly k.
Input
Integers n (1 ≤ n ≤ 10^5) and k (1 ≤ k ≤ n), and an array counts of n integers (0 ≤ counts[i] ≤ 10^6).
Output
A single integer — the maximum window sum.
Constraints
- 1 ≤ k ≤ n ≤ 100,000
- 0 ≤ counts[i] ≤ 1,000,000
- Window length is exactly k (not 'at most k')
Examples
Example 1
Input
counts = [2, 1, 5, 1, 3, 2], k = 3
Output
9
Windows of size 3 sum to 8, 7, 9, 6. The window [5,1,3] gives the maximum, 9.
Example 2
Input
counts = [4, 4, 4], k = 2
Output
8
Both size-2 windows sum to 8.