Maximum Average Subarray I

Easy
1
41.2% Acceptance

In this lab, you will work on an algorithm that finds the contiguous subarray of a given length k with the maximum average value. This is an important and useful problem in many daily life scenarios related to finance, data analysis, etc.

You will be given an integer array nums consisting of n elements and an integer k. Your task is to 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.

For example:

Input: nums = [1,12,-5,-6,50,3], k = 4 Output: 12.75000 Explanation: Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75

Input: nums = [4,2,1,3,4], k = 2 Output: 3.50000 Explanation: Maximum average is (3 + 4) / 2 = 3.5

In this lab, you will write a function named findMaxAverage that takes two parameters:

  1. nums: An integer array consisting of n elements.
  2. k: The length of the subarray to be found.

Function signature:

/** * @param {number[]} nums * @param {number} k * @return {number} */ var findMaxAverage = function(nums, k) { };

Make sure to properly import and export the findMaxAverage function so it can be tested in the evaluation script.

Challenges

  1. Implement the findMaxAverage function and make sure it returns the maximum average for a given subarray of length k.
  2. Test the function with multiple test cases to ensure its correctness, and handle edge cases if necessary.
  3. Export the findMaxAverage function properly, so it can be imported in the evaluation script.

Now, complete the lab by writing the evaluation script, making sure it properly tests your implementation and properly logs the test results. Also, make sure to follow the rules we discussed earlier for building this lab. Good luck!