Watch 10 video solutions for Maximum Average Subarray I, a easy level problem involving Array, Sliding Window. This walkthrough by Nikhil Lohia has 46,442 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an integer array nums consisting of n elements, and an integer k.
Find a contiguous subarray whose length is 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.
Example 1:
Input: nums = [1,12,-5,-6,50,3], k = 4 Output: 12.75000 Explanation: Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75
Example 2:
Input: nums = [5], k = 1 Output: 5.00000
Constraints:
n == nums.length1 <= k <= n <= 105-104 <= nums[i] <= 104Problem Overview: You are given an integer array nums and an integer k. The task is to find the contiguous subarray of length k that has the maximum average value and return that average. Since the subarray size is fixed, the main challenge is computing sums efficiently without recomputing the same elements repeatedly.
Approach 1: Brute Force (Time: O(n*k), Space: O(1))
The most direct method checks every subarray of size k. Start from index 0, sum the next k elements, compute the average, and track the maximum value. Move the starting index one step to the right and repeat until all possible windows are processed. Each window requires summing k elements, which leads to O(n*k) time complexity for an array of length n. Space usage stays O(1) since only a few variables store the running sum and maximum average.
This approach is easy to implement and helps you understand the problem mechanics. However, it repeatedly recalculates overlapping sums. For large arrays or large k, the redundant computation becomes expensive. The inefficiency comes from recomputing nearly the same window sum every iteration.
Approach 2: Sliding Window Technique (Time: O(n), Space: O(1))
The optimal solution uses a sliding window over the array. Instead of recalculating the entire sum for every window, compute the sum of the first k elements once. This represents the first window. Then slide the window forward by one position: subtract the element leaving the window and add the new element entering the window.
This update takes constant time. As you move the window from left to right, maintain the maximum window sum encountered. After processing the entire array, divide the maximum sum by k to obtain the maximum average. Because each element enters and leaves the window at most once, the algorithm runs in O(n) time with O(1) extra space.
The key insight is that consecutive windows share k-1 elements. Updating the sum incrementally avoids redundant work. This pattern appears frequently in problems involving fixed-size subarrays, streaming data, or running aggregates. Sliding window is often paired with array traversal problems and is a fundamental technique for interview preparation.
Recommended for interviews: Interviewers expect the Sliding Window approach. The brute force solution shows you understand the problem definition, but it does not scale well. The sliding window demonstrates that you recognize overlapping computations and can optimize them to achieve O(n) time complexity with constant space.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force | O(n*k) | O(1) | Good for understanding the problem or when k is extremely small |
| Sliding Window | O(n) | O(1) | Best choice for fixed-size subarray problems and interview solutions |