You are given an integer array nums consisting of n elements, and an integer k.
Find a contiguous subarray whose length is equal to k that has the maximum average value and return this value. Any answer with a calculation error less than 10-5 will be accepted.
Example 1:
1 | Input: nums = [1,12,-5,-6,50,3], k = 4 |
Example 2:
1 | Input: nums = [5], k = 1 |
Constraints:
1 | n == nums.length |
Approach
1 | Calculate the average of every k elements and find the max value. |
Algorithm
1 | Go through the list with a sliding window of size k and keep the max value. |
Implementation
1 | #include <float.h> |