Algorithms Playbook
← All problems
EasySliding window →Similar to: Maximum Average Subarray I

Best stretch of k

Problem

Given an array nums and an integer k, return the largest average value among all contiguous subarrays of length exactly k.

Examples

Input: nums = [1, 12, -5, -6, 50, 3], k = 4

Output: 12.75

[12, -5, -6, 50] sums to 51; 51 / 4 = 12.75.

Input: nums = [5], k = 1

Output: 5.0

Hints

    Try it first. Every hint reveals a bit more.

    Solution