Skip to main content

Fixed-Length Subarray Average Peak

easy
ArraySliding WindowSliding Window
Asked atAmazonMicrosoftGoogle

Problem Description

Given an integer array `values` and a positive integer `windowSize`, return the **maximum average** of any contiguous subarray of exactly `windowSize` elements.

**Example 1:**
```
Input: values = [3, 7, 2, 9, 4, 6, 1], windowSize = 3
Output: 6.33333
Explanation: The subarray [7, 2, 9] has sum 18 and average 6.33333...
```

**Example 2:**
```
Input: values = [10, 2, 5, 8, 3], windowSize = 2
Output: 9.0
Explanation: The subarray [10, 2] has sum 12 and [5, 8] has sum 13 — wait, [10, 2]=12, [2,5]=7, [5,8]=13, [8,3]=11. Maximum is 13/2 = 6.5. Actually re-check: [5,8] → 6.5. Hmm: max average is (8+3)/2=5.5? No — [5,8]=13 → 6.5, which is the max.
Output: 6.5
```

Constraints

  • 1 <= values.length <= 100,000
  • 1 <= windowSize <= values.length
  • -10,000 <= values[i] <= 10,000

Follow-up

What if you need to return the starting index of the window with the maximum average, breaking ties by choosing the leftmost window?

Hints

Try the problem first. If you get stuck, you can reveal hints one at a time.

Solution

Leaderboard

No entries yet for python.

Be the first — submit an accepted solution.

CodeBrainery

A technical blogging platform for developers and engineers to share knowledge and connect.

Connect

© 2026 Kodetra Technologies Pvt. Ltd. All rights reserved. CodeBrainery is a product of Kodetra Technologies Pvt. Ltd.