Data Structures & Algorithms Sliding Window
💡
Exercise 42

Max Average Subarray 15 XP Easy

LeetCode Ctrl+Enter Run Ctrl+S Save

Given an integer array nums and integer k, find the contiguous subarray of length k with the maximum average value.

nums = [1,12,-5,-6,50,3], k = 4 # Subarray [12,-5,-6,50] has sum 51 # Average = 51/4 = 12.75

This is a classic fixed-size sliding window problem. Track the sum and divide by k at the end.

💡 Pro tip: Understand this problem deeply — don't just memorize the code. Try explaining the approach out loud as if teaching a friend. If you can explain it simply, you truly understand it!

📋 Instructions
Implement `find_max_average(nums, k)` using a sliding window. Test with `nums = [1,12,-5,-6,50,3]` and `k = 4`. Print the result (as a float).
Same as the intro exercise but divide the max sum by k at the end to get the average.
🧪 Test Cases
Input
find_max_average([1,12,-5,-6,50,3], 4)
Expected
12.75
Test case 1
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.