Lazy Sliding-Window Maximum & Moving Average Pipeline

Difficulty: Hard · Unit 5: OOP, Iterators & Complexity · Featured in: Tough Challenge

Given a stream of N integers and window size K (1 <= K <= N), build a lazy generator yielding `(window_max, round(window_avg, 2))` for every contiguous window of size K in O(N) total time (NOT O(N * K)).

Input & Output Format

Input: Line 1: two integers N and K. Line 2: N space-separated integers.

Output: Print one line per window: `<max> <avg:.2f>`.

Sample 1

6 3
1 3 -1 -3 5 3
3 1.00
3 -0.33
5 0.33
5 1.67

Windows: [1,3,-1] max=3 avg=1.00; [3,-1,-3] max=3 avg=-0.33; [-1,-3,5] max=5 avg=0.33; [-3,5,3] max=5 avg=1.67.

Free forever — sign in with email at poorvithmp.com to unlock the dry run, verified Python 3 solution, edge-case traps, and downloads.

Back to Python Notes Hub