A solar array logs its hourly energy output for n consecutive hours. A maintenance report needs the busiest stretch: over every block of exactly k consecutive hours, find the one with the greatest total output and report that total.
Line 1: two integers n and k.
Line 2: n space-separated non-negative integers, the hourly outputs.
A single integer: the maximum sum of any k consecutive hourly outputs.
Example 1
Input
5 2 3 1 4 1 5
Expected
6
Explanation
The four 2-hour windows sum to 4, 5, 5, and 6; the largest is the last pair (1 + 5 = 6).
Example 2
Input
4 4 2 3 4 1
Expected
10
Explanation
With k equal to n there is one window, the whole array, summing to 10.
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 →