Find the maximum average value among all contiguous subarrays of length k.
Problem
Given an integer array nums and an integer k, consider every contiguous subarray of length exactly k. Your task is to compute the largest possible average among them.
Because averages can be fractional, return the answer as a real number.
Clarification
A subarray must consist of consecutive elements, and only subarrays with length exactly k are considered.
Input Format
nums: an integer arrayk: the required subarray length
The exact runtime input format varies by platform; conceptually the function receives these two values.
Output Format
- Return a floating-point number representing the maximum average over all length-
ksubarrays.
Constraints
1 <= k <= nums.lengthnums.lengthis at leastk- Values may be positive, negative, or zero
- Answer is judged with a small floating-point tolerance
Example 1
Input
nums = [1, 12, -5, -6, 50, 3], k = 4
Output
12.75
Explanation
The length-4 subarrays are [1,12,-5,-6], [12,-5,-6,50], [-5,-6,50,3]. Their averages are 0.5, 12.75, and 10.5 respectively, so the maximum is 12.75.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.