Highest Average

Start Timer

0:00:00

Upvote
1
Downvote
Save question
Mark as completed
View comments

You are given an integer array nums consisting of n elements, which you can think of as a long sequence of values laid out in order, and an integer k that represents a fixed window size.

As you slide this window across the array, you want to observe every contiguous subarray of length exactly k and determine which one produces the highest average value.

Your task is to find that maximum average and return it. Any answer with a calculation error less than 10⁻⁵ will be accepted, so a small margin of numerical imprecision is allowed.

Example:

Input:

nums = [2,26,-5,-3,57,3,8,5], k = 4

Output:

def highest_average(nums, k) -> 18.75000

Explanation: Maximum average is (26-5-3+57)/4 = 75/4 = 18.75

Constraints:

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


Comments

Loading comments