Problem
You get an integer array nums and an integer k. A subarray is a part of the array with consecutive items.
Find the subarray of length k that has the largest average value. Return this average. The answer can have a small rounding error.
Example 1
- Input:
- nums = [3,-2,7,2,5], k = 2
- Output:
- 4.5
- Explanation:
- The subarray
[7, 2]has the sum 9, so its average is 4.5.
Example 2
- Input:
- nums = [-4,-1,-6,-3], k = 2
- Output:
- -2.5
- Explanation:
- The subarray
[-4, -1]has the largest average, which is -2.5.
Example 3
- Input:
- nums = [2,8,4,6], k = 4
- Output:
- 5.0
- Explanation:
- The only subarray of length 4 is the whole array. Its average is
20 / 4, which is 5.
Constraints
- n == nums.length
- 1 <= k <= n <= 10^5
- -10^4 <= nums[i] <= 10^4
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
Oracle
Leetcode 643. Maximum Average Subarray I
Reported Feb 8, 2026 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View source