Maximum Average Subarray I

Easy#643fixed size

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 of nums.
returns
The largest average of k numbers in a row in nums.
constraints
  • 1 ≤ k ≤ nums.length ≤ 10⁴
  • −10⁴ ≤ nums[i] ≤ 10⁴
our 11 tests keep to them
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

11 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more sliding window problems

Pattern “Sliding window” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.