Maximum Average Subarray I

You are given an integer array nums consisting of n elements, and an integer k.

Find a contiguous subarray whose length is equal to k that has the maximum average value and return this value. Any answer with a calculation error less than 10^-5 will be accepted.

Example 1
Inputnums = [1,12,-5,-6,50,3], k = 4
Output12.75
Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75.
Example 2
Inputnums = [5], k = 1
Output5
The only contiguous subarray of length 1 is [5], so the maximum average is 5.0.

Constraints

  • n == nums.length
  • 1 <= k <= n <= 10^5
  • -10^4 <= nums[i] <= 10^4

Asked at 11 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate