Maximum Average Subarray
You are given an array of integers nums and an integer k.
Among all contiguous subarrays of length exactly k, find the one with the largest average and return that average. Answers within 10^-5 of the exact value are accepted.
Constraints
1 <= k <= nums.length <= 10^5-10^4 <= nums[i] <= 10^4
Examples
Example 1
Input: nums = [4,0,6,-2,9,1], k = 3 Output: 4.333333333
The windows of length 3 sum to 10, 4, 13 and 8. The best is 6 + -2 + 9 = 13, and 13 / 3 ≈ 4.33333.
Example 2
Input: nums = [5], k = 1 Output: 5
The only window is [5].
Example 3
Input: nums = [-1,-2,-3], k = 2 Output: -1.5
All values are negative: the best window is [-1, -2], with average -1.5.
JavaScript you'll use: Arrays: create, read, update Numbers, Math and integer tricks
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
function findMaxAverage(nums, k) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.