52. Maximum Average Subarray I

EasyArrayArray

You are given an integer array nums consisting of n elements and an integer k. Find a contiguous subarray of length k that has the maximum average value. Return this maximum average.

Input: An integer array nums and an integer k.

Output: Float — maximum average of any k-length subarray.

Examples

Example 1
Input: [1,12,-5,-6,50,3],4
Output: 12.75000
Explanation: Subarray [12,-5,-6,50] has sum=51, average=12.75.
Example 2
Input: [5],1
Output: 5.00000
Explanation: Only one subarray of length 1: [5]. Average=5.
Example 3
Input: [1,2,3,4,5],3
Output: 4.00000
Explanation: Best k=3 subarray: [3,4,5] with average=4.

Constraints

Asked by

MetaBloombergGoogleAmazonMicrosoftInfosys
Solve this problem in the editor →