A sensor logs one integer energy reading per second. Given a fixed window length k, you want to find the single window of k consecutive readings whose total energy is the largest.
Return that maximum total. Readings may be negative (energy can be drained).
Line 1: two integers n and k separated by a space, where n is the number of readings and k is the window length.
Line 2: n space-separated integers, the readings in order.
A single integer: the maximum sum over all contiguous windows of exactly k readings.
Example 1
Input
6 3 2 1 5 1 3 2
Expected
9
Explanation
Windows of length 3 sum to 8, 7, 9, 6. The largest is [5,1,3] = 9.
Example 2
Input
5 2 -4 -2 -7 -1 -3
Expected
-4
Explanation
All readings are negative; the length-2 windows sum to -6, -9, -8, -4. The maximum is [-1,-3] = -4.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →