All problems

Maximum Average Subarray I

Easy · sliding window

A weather station logs nums, one temperature reading per hour (readings can be negative). You want the warmest stretch of exactly k consecutive hours, meaning the stretch with the highest average.

Because every stretch has the same length k, the stretch with the highest average is also the one with the highest sum. To avoid fractions, return that highest sum of k consecutive readings.

Examples

Example 1

Input: nums = [2, -1, 4, 3, -2, 5], k = 3

Output: 6

The four windows sum to 5, 6, 5 and 6, so the best sum is 6.

Example 2

Input: nums = [-4, -2, -9], k = 2

Output: -6

Example 3

Input: nums = [8], k = 1

Output: 8

Constraints