Given an array of integers arr and two integers k and threshold, return the number of sub-arrays of size k and average greater than or equal to threshold.
Example 1:
Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold).
Example 2:
Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers.
Constraints:
1 <= arr.length <= 1051 <= arr[i] <= 1041 <= k <= arr.length0 <= threshold <= 104When 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:
The brute force approach to this problem is very straightforward. We're going to look at every possible group of numbers of the correct size and check each one to see if its average is good enough. If it is, we count it.
Here's how the algorithm would work step-by-step:
def number_of_subarrays(
numbers, subarray_length, threshold
):
number_of_subarrays_meeting_criteria = 0
# Iterate through the array, considering each possible start
for start_index in range(len(numbers) - subarray_length + 1):
current_subarray_sum = 0
# Calculate the sum of the current sub-array.
for index_in_subarray in range(subarray_length):
current_subarray_sum += numbers[
start_index + index_in_subarray
]
# Calculate the average of the current sub-array.
current_subarray_average =
current_subarray_sum / subarray_length
# Check if the average meets the specified threshold.
if current_subarray_average >= threshold:
# Increment the result if the criteria is met.
number_of_subarrays_meeting_criteria += 1
return number_of_subarrays_meeting_criteriaThe goal is to efficiently count segments of a specific length within a set of numbers that meet a certain average requirement. The core idea is to avoid recomputing the sum of each segment from scratch, instead cleverly reusing previous calculations.
Here's how the algorithm would work step-by-step:
def number_of_subarrays(input_array, k_length, threshold):
number_of_subarrays_result = 0
current_sum = 0
# Calculate the sum of the first k_length elements
for i in range(k_length):
current_sum += input_array[i]
# Check if the average of the first subarray meets the threshold
if current_sum / k_length >= threshold:
number_of_subarrays_result += 1
# Iterate through the rest of the array
for i in range(k_length, len(input_array)):
# Efficiently update the sum by subtracting the leftmost and adding the next element
current_sum -= input_array[i - k_length]
current_sum += input_array[i]
# Check if the average of the current subarray meets the threshold
if current_sum / k_length >= threshold:
number_of_subarrays_result += 1
return number_of_subarrays_result| Case | How to Handle |
|---|---|
| Empty input array | Return 0, since no subarrays can be formed from an empty array. |
| K is zero or negative | Return 0, since a subarray of non-positive size is impossible. |
| K is larger than the size of the input array | Return 0, since no subarrays of size K can be formed if K exceeds the array's size. |
| Input array contains negative numbers | The algorithm should correctly handle negative numbers in the average calculation. |
| Input array contains very large numbers that could cause overflow during sum calculation | Use a data type with sufficient range (e.g., long) to store the sum and consider dividing by K first to minimize overflow. |
| Threshold is very large or very small (close to infinity or negative infinity) | The comparison `average >= threshold` should still hold true for extreme threshold values. |
| All elements are identical and less than the threshold | Return 0 if the identical value is less than the threshold, as no subarray's average will reach the threshold. |
| K is equal to the length of the array | The average of the entire array should be compared to the threshold. |