You are given an integer array nums
consisting of n
elements, and an integer k
.
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:
nums = [1, 12, -5, -6, 50, 3], k = 4
The maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75
So the correct output is 12.75000
As another example:
nums = [5], k = 1
In this case, the correct output is 5.00000
How would you solve this problem efficiently?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to find the best small group of numbers within a larger list of numbers. The brute force approach looks at every single possible group of the required size, without skipping any.
Here's how the algorithm would work step-by-step:
def find_max_average_brute_force(number_list, subarray_length):
maximum_average = float('-inf')
# Iterate through all possible starting positions of the subarray
for i in range(len(number_list) - subarray_length + 1):
# Calculate the sum of the current subarray
current_subarray_sum = 0
for j in range(subarray_length):
current_subarray_sum += number_list[i + j]
#Calculate average of current subarray
current_average = current_subarray_sum / subarray_length
# Update maximum_average if current average is greater
maximum_average = max(maximum_average, current_average)
return maximum_average
The most efficient way to find the maximum average of a continuous group of numbers is by using a sliding window technique. Imagine a window of a fixed size moving across the numbers. By only updating the sum within the window, we avoid recalculating the entire average each time, making the process much faster.
Here's how the algorithm would work step-by-step:
def find_max_average(numbers, subarray_length):
current_subarray_sum = sum(numbers[:subarray_length])
max_subarray_sum = current_subarray_sum
# Start sliding window after initial subarray
for i in range(subarray_length, len(numbers)):
current_subarray_sum += numbers[i] - numbers[i - subarray_length]
# Keep track of the largest subarray sum
max_subarray_sum = max(max_subarray_sum, current_subarray_sum)
# Calculating average after iterating saves time
maximum_average = max_subarray_sum / subarray_length
return maximum_average
Case | How to Handle |
---|---|
Empty input array | Return 0 or throw an IllegalArgumentException as there's no subarray. |
k is larger than the array size | Return 0 or throw an IllegalArgumentException as a subarray of size k is impossible. |
Array size equals k (subarray is the whole array) | Calculate the average of the entire array. |
Array contains large positive and negative numbers leading to potential overflow when summing | Use long type to store the sum to avoid integer overflow. |
k is 1 (single element subarray) | Return the maximum element in the array, as it will have the highest average. |
All elements in the array are negative | The algorithm should correctly identify the subarray with the least negative (closest to zero) average. |
All elements in the array are zero | The average will be zero regardless of the subarray, so return 0. |
Floating-point precision issues with very large or small average values | Consider using double for intermediate calculations and result to minimize precision loss. |