
Leetcode Problem 643: Maximum Average Subarray I
Given an integer array nums and an integer k, find the contiguous subarray of length exactly k with the maximum average value and return that average. Answers within 10^-5 of the true value are accepted.
Constraints:
k is between 1 and the array length.function findMaxAverage(nums: number[], k: number): number {
let sum = 0
for (let i = 0; i < k; i++) {
sum = sum + nums[i]
}
let maxSum = sum
for (let i = k; i < nums.length; i++) {
sum = sum - nums[i - k] + nums[i]
maxSum = Math.max(sum, maxSum)
}
return maxSum / k
};
console.log(findMaxAverage([1,12,-5,-6,50,3], 4))