You are given an integer array nums consisting of n elements and an integer k. Your task is to find a contiguous subarray whose length is greater than or equal to k that has the maximum average value.
Unlike the simpler version where the subarray length is fixed, this problem allows variable-length subarrays (as long as they're at least k elements long). This makes it significantly more challenging because you need to consider all possible subarray lengths from k to n.
Example: Given nums = [1,12,-5,-6,50,3] and k = 4, you need to find the subarray of length โฅ 4 with the highest average. The subarray [12,-5,-6,50] has average 12.75, which turns out to be optimal.
Return the maximum average value as a double. Any answer within 10-5 of the actual answer will be accepted.
Input & Output
Constraints
- n == nums.length
- 1 โค k โค n โค 105
- -104 โค nums[i] โค 104
- Answer precision: Any answer within 10-5 of the actual answer will be accepted