Maximum Average Subarray I
Problem Statement
Given an integer array nums and an integer k, find a contiguous subarray of length exactly k that has the maximum average value. Return that maximum average value.
Input
An integer array nums and an integer k, the required fixed window length.
Output
A decimal number: the maximum average among all contiguous subarrays of length k.
Constraints
- •
1 <= k <= nums.length <= 10^5 - •
-10^4 <= nums[i] <= 10^4
Examples
Example 1
nums = [1,12,-5,-6,50,3], k = 4
12.75000Example 2
nums = [5], k = 1
5.00000Example 3
nums = [-1,-12,-5,-6], k = 2
-5.50000Learning Objectives
- Recognise an exactly **k** sized contiguous subarray as a fixed-window problem.
- Replace repeated window re-summing with one running sum that updates in O(1).
- Track the best sum first, then convert it to an average only once at the end.
- Handle negative values by initialising the best answer from the first complete window.
Intuition
The pattern signal is strong: the problem asks for a contiguous subarray with length exactly k. That means every candidate window has the same size, and each next window differs from the previous one by only two values: one leaves from the left and one enters from the right.
A tempting approach is to compute the sum of every length-k subarray from scratch. That repeats almost all of the same additions. Fixed window removes the waste: compute the first window once, then slide one step at a time by subtracting the leaving value and adding the entering value.
Common mistakes
- ×Recomputing every length-**k** sum from scratch, which wastes O(k) work per window.
- ×Dividing on every slide and comparing floating-point values instead of comparing integer sums.
- ×Initialising the best sum to **0**, which fails when every possible window has a negative sum.
- ×Removing the wrong left element after the right pointer advances.
Algorithm Explanation
Window setup
Maintain windowSum, the sum of the current length-k window. Because the window size never changes, maximising the average is the same as maximising the sum. Initialise windowSum with the first k elements and set bestSum to that value.
Window visualization
For nums = [1,12,-5,-6,50,3] and k = 4, the first window is [1,12,-5,-6] with sum 2. Slide right by one position: 1 leaves, 50 enters, and the new window [12,-5,-6,50] has sum 51. Slide again: 12 leaves, 3 enters, and [-5,-6,50,3] has sum 42. The best sum is 51, so the best average is 51 / 4 = 12.75.
Algorithm
- Sum the first k elements to form the first complete window.
- Store that sum as bestSum.
- For each right index from k to the end of the array, add nums[right] to include the entering value.
- Subtract nums[right - k] to remove the value that just left the window.
- Update bestSum if the current window sum is larger.
- Return bestSum / k as a double.
Solutions
Solution: Fixed-size running sum
Use one running sum for the current length-k window. Each slide updates that sum in constant time by adding the new right value and subtracting the old left value.
Step-by-step
- Compute the sum of indices 0 through k - 1.
- Set bestSum to the first complete window sum so negative arrays are handled correctly.
- Starting at index k, slide the window right by adding the entering element and subtracting the element k positions behind it.
- Keep the largest window sum seen.
- Convert the best sum to a decimal average in the return statement.
O(n)
O(1)
Each element enters the running sum once and leaves it at most once.
Java implementation
Dry Run
Sample input
nums = [1,12,-5,-6,50,3], k = 4. Track the fixed window sum as each slide removes one value and adds one value.
| step | entering value | leaving value | window sum | best sum |
|---|---|---|---|---|
| initial window [1,12,-5,-6] | 1, 12, -5, -6 | none | 2 | 2 |
| right = 4 | 50 | 1 | 51 | 51 |
| right = 5 | 3 | 12 | 42 | 51 |
The largest length-4 window sum is 51, so the maximum average is 51 / 4 = 12.75.
Interview Tips
Say the key reduction out loud: for a fixed k, the denominator never changes, so maximising average is equivalent to maximising sum. This avoids floating-point comparison during the scan. Also call out the all-negative case, because correct initialisation from the first complete window is a common interview check.
Likely follow-ups
- How would the answer change if the window size could be at most **k** instead of exactly **k**?
- How would you return the start index of the best window as well as the average?
- How would you process the same query for many different values of **k**?
- What if the input arrives as a stream and you need the best length-**k** average seen so far?
Similar Problems
Key Takeaways
- Exactly **k** elements is the strongest signal for a fixed-size window.
- Adjacent fixed windows differ by one leaving value and one entering value.
- Compare sums while scanning, then divide once to produce the average.
- Initialise from the first real window, not from a neutral value like **0**.