Find the Maximum Average Value in an Array

Given an array of n integers, find the contiguous subarray of given length k that has the maximum average value. Output the maximum average value.

Problem

Given an array of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average value.
Example input: [1,12,-5,-6,50,3], k = 4
Example output: 12.75

Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by kayakane
def max_average(arr, k):
    max_sum = 0
    for i in range(len(arr) - k + 1):
        max_sum = max(max_sum, sum(arr[i:i+k]))
    return max_sum / k

print(max_average([1,12,-5,-6,50,3], 4))

A.I. Evaluation of the Solution

The candidate's solution correctly finds the maximum average value of a given array and length k. The solution is clear and easy to follow. One potential improvement would be to use the built-in max() and sum() functions rather than creating a custom max_sum function.

Evaluated at: 2022-12-11 08:15:23