Maximum Average Subarray I
You get an array of numbers and a length k. Among all runs of k numbers in a row, find the largest average. Our example: [5, -3, 4, 8, -2, 6, -7] and k = 4 — the answer is 4: the run [4, 8, -2, 6] sums to 16, and 16 / 4 = 4.
- input
nums: integers;k: the length of a run, at most the length ofnums.- returns
- The largest average of
knumbers in a row innums. - constraints
- 1 ≤ k ≤ nums.length ≤ 10⁴
- −10⁴ ≤ nums[i] ≤ 10⁴
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
function findMaxAverage(nums, k) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more sliding window problems
- 28Find the Index of the First Occurrence in a StringEasysolve
- 3Longest Substring Without Repeating CharactersMediumsolve
- 209Minimum Size Subarray SumMediumsolve
- 424Longest Repeating Character ReplacementMediumsolve
- 567Permutation in StringMediumsolve
- 76Minimum Window SubstringHardsolve
- 30Substring with Concatenation of All WordsHardsolve
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.