LeetCode 644, Hard. Topics: Array, Binary Search, Prefix Sum. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 15 parametrized pytest cases, and a playground notebook:
Problem
You are given an integer array nums consisting of n elements, and an integer k.
Find a contiguous subarray whose length is greater than or equal to k that has the maximum average value and return this value. Any answer with a calculation error less than 10^-5 will be accepted.
Examples
Constraints
- n == nums.length
- 1 <= k <= n <= 10^4
- -10^4 <= nums[i] <= 10^4
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026