You are given an array of integers and a fixed length k. Consider every contiguous block of exactly k consecutive elements and compute the average of each block.
Return the largest average found among all such blocks. Answers within a tiny numerical tolerance of the true maximum are accepted.
Example 1
Input: nums = [1, 12, -5, -6, 50, 3], k = 4
Output: 12.75
The window [12, -5, -6, 50] sums to 51, and 51 / 4 = 12.75, which beats every other length-4 window.
Example 2
Input: nums = [4, 0, 4], k = 2
Output: 2.00000
The two length-2 windows are [4, 0] and [0, 4], both averaging 2. The maximum average is 2.
Constraints
1 <= k <= nums.length <= 10^5-10^4 <= nums[i] <= 10^4See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI