13 · Sliding Window

#643 Maximum Average Subarray I

Easysolve on LeetCode ↗

frequently reported atMetaBloombergGoogleAmazonMicrosoft

the problem

In plain words

Find the contiguous subarray of length exactly k with the largest average, and return that average.

in nums = [1,12,-5,-6,50,3], k = 4

out 12.75

(12 − 5 − 6 + 50) / 4 = 51 / 4 = 12.75.

in nums = [5], k = 1

out 5.0

  • 1 ≤ k ≤ n ≤ 10⁵
  • -10⁴ ≤ nums[i] ≤ 10⁴

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #209
      Minimum Size Subarray Sum
      Mediumin Sliding Window
    2. #303
    3. #239