Code
Loading editor...
Tap Analyze to see visualization
Click Analyze to visualize
See step-by-step execution, variables, and output
Variables
Run code to see variables
Output
Console output will appear here
Click Analyze to visualize
See step-by-step execution, variables, and output
Run code to see variables
Console output will appear here
Use the same fixed-size sliding window as Maximum Sum Subarray. Build the first window of k elements, then slide across, tracking the maximum sum. At the end, divide the maximum sum by k to get the maximum average.
Since k is constant, the subarray with the maximum sum also has the maximum average. We only need to divide once at the end rather than at each step, keeping the logic simple and efficient.
Updated Feb 2026