left
as the minimum value in nums
and right
as the maximum value in nums
.right - left
is greater than 10^-5
, do the following:
a. Calculate mid
as (left + right) / 2
.
b. Initialize sum
and prev
to 0, and min_sum
to 0.
c. Iterate through the array, updating sum
by adding nums[i] - mid
.
d. If i
is greater than or equal to k
, update min_sum
to the minimum of min_sum
and prev
, and update prev
by adding nums[i - k] - mid
.
e. If sum
is greater than or equal to min_sum
, set left
to mid
. Otherwise, set right
to mid
.left
as the maximum average subarray value.