Loading content...
You are given an integer array arr, a positive integer k representing the window size, and an integer threshold representing the minimum required average.
Your task is to count the total number of contiguous segments (subarrays) of exactly k consecutive elements where the arithmetic mean of those elements is greater than or equal to the specified threshold.
A contiguous segment is defined as a sequence of consecutive elements from the array. For a segment starting at index i and ending at index i + k - 1, the arithmetic mean is calculated as the sum of all elements in that range divided by k.
Return the count of all such qualifying segments.
arr = [2,2,2,2,5,5,5,8]
k = 3
threshold = 43We need to find contiguous segments of size 3 with average ≥ 4. The valid segments are: • [2,5,5] at indices [3,4,5]: sum = 12, average = 4.0 ✓ • [5,5,5] at indices [4,5,6]: sum = 15, average = 5.0 ✓ • [5,5,8] at indices [5,6,7]: sum = 18, average = 6.0 ✓ Other segments like [2,2,2], [2,2,5], [2,5,5] (at earlier positions) have averages less than 4.
arr = [11,13,17,23,29,31,7,5,2,3]
k = 3
threshold = 56The first 6 contiguous segments of size 3 all have averages greater than or equal to 5: • [11,13,17]: average ≈ 13.67 • [13,17,23]: average ≈ 17.67 • [17,23,29]: average = 23.0 • [23,29,31]: average ≈ 27.67 • [29,31,7]: average ≈ 22.33 • [31,7,5]: average ≈ 14.33 The remaining segments [7,5,2] and [5,2,3] have lower averages.
arr = [5,5,5,5,5]
k = 2
threshold = 54All possible segments of size 2 have exactly average = 5, which meets the threshold: • [5,5] at indices [0,1]: average = 5.0 ✓ • [5,5] at indices [1,2]: average = 5.0 ✓ • [5,5] at indices [2,3]: average = 5.0 ✓ • [5,5] at indices [3,4]: average = 5.0 ✓ Total count = 4.
Constraints