EasyArraySliding WindowUpdated Sep 2026

643. Maximum Average Subarray I

Asked at Amazon

Difficulty

Easy

Time

O(n)

Space

O(1)

Pattern

Array

Companies

1

Problem

You get an integer array nums and an integer k. Your job is to find a contiguous block of exactly k elements (they must sit next to each other) whose average is as large as possible, and return that maximum average.

Because every block has the same length k, comparing averages is the same as comparing sums. So you can look for the block with the largest sum, then divide that sum by k at the end.

Return the answer as a decimal number. A small rounding error is fine — any answer within 10^-5 of the true value is accepted.

Pick the best block of length k

numssum 51112-5-6503012345The window [12,-5,-6,50] sums to 51, the largest ofall length-4 windows.numssum 2112-5-6503012345For comparison, the first window [1,12,-5,-6] onlysums to 2.
Every block must have exactly k = 4 elements. The block [12,-5,-6,50] has the biggest sum, so it wins.

Constraints

  • ●n == nums.length
  • ●1 <= k <= n <= 10^5
  • ●-10^4 <= nums[i] <= 10^4

Examples

Example 1
Inputnums = [1,12,-5,-6,50,3], k = 4
Output12.75000
WhyThe best block of 4 is [12,-5,-6,50]. Its sum is 51, and 51 / 4 = 12.75. No other block of length 4 is larger.
Example 2
Inputnums = [5], k = 1
Output5.00000
WhyThere is only one element, so the only block is [5]. Its average is 5.

Asked At

1 companies
CompanyDifficulty
AmazonEasyView all Amazon questions →

How to Think About It

1

Brute force: compute the sum of every subarray of length k and track the maximum.

2

Optimize by sliding the window: subtract the element leaving and add the element entering.

3

Precompute the prefix sum of the first k elements as the initial window sum.

4

Slide the window one position at a time updating the sum in O(1).

5

Return the maximum sum divided by k. Time O(n), space O(1).

Optimal Approach

Compute the sum of the first k elements. Then slide the window one element at a time: add the new element on the right and subtract the element that falls off on the left. Track the maximum sum seen. The answer is the maximum sum divided by k. This runs in O(n) time with O(1) space. The fixed window makes this simpler than variable-size sliding window problems.

Slide the window one step at a time

numssum 2112-5-6503012345Start: the first window [1,12,-5,-6] has sum 2.numssum 51112-5-6503012345Slide: subtract nums[0]=1, add nums[4]=50, so the sumbecomes 2 - 1 + 50 = 51.numssum 42112-5-6503012345Slide again: subtract nums[1]=12, add nums[5]=3,giving 51 - 12 + 3 = 42.window_sum += nums[i] - nums[i-k]max_sum = max(max_sum, window_sum)answer = max_sum / k
Instead of re-adding k numbers each time, drop the left element and add the new right element.
ComplexityTime O(n)Space O(1)We touch each element once while sliding the window, and we only keep a running sum and the best sum.

What Trips People Up in Real Interviews

1

Confirm that k is guaranteed to be less than or equal to nums.length.

2

Ask whether floating point precision matters for the average.

3

Mention that this is a fixed-size sliding window (unlike variable-size variants).

4

Edge cases: k equals 1 (return max element), k equals n (return total average).

5

Code the sum update pattern: sum += nums[i] - nums[i - k] to avoid recomputation.

Solution Code

O(n) time
def findMaxAverage(nums, k):
    window_sum = sum(nums[:k])
    max_sum = window_sum
    for i in range(k, len(nums)):
        window_sum += nums[i] - nums[i - k]
        max_sum = max(max_sum, window_sum)
    return max_sum / k

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Maximum Average Subarray I problem?

Given an integer array nums and an integer k, find a contiguous subarray of length k that has the maximum average value. Return the maximum average value. This is a straightforward application of the fixed-size sliding window pattern.

How do you solve Maximum Average Subarray I?

The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Maximum Average Subarray I?

Maximum Average Subarray I is asked at Amazon. It is a easy difficulty problem.

What are common mistakes on Maximum Average Subarray I?
  • Confirm that k is guaranteed to be less than or equal to nums.length.
  • Ask whether floating point precision matters for the average.
  • Mention that this is a fixed-size sliding window (unlike variable-size variants).
  • Edge cases: k equals 1 (return max element), k equals n (return total average).
  • Code the sum update pattern: sum += nums[i] - nums[i - k] to avoid recomputation.