We’re preparing your current view and syncing the latest data.
Count subarrays with average above threshold
gfgYou are given an integer array nums and two integers k and threshold. Consider all contiguous subarrays of nums of size k. Your task is to find how many subarrays have an average value greater than or equal to threshold. The average of a subarray is defined as the sum of its elements divided by k.
Return the count of such subarrays.
An integer array nums, followed by two integers k and threshold.
An integer representing the number of subarrays of size k with an average greater than or equal to threshold.
1 <= nums.length <= 10^5 1 <= k <= nums.length 0 <= nums[i], threshold <= 10^4
Example 1
Input
nums = [2,2,2,2,5,5,5,8], k = 3, threshold = 4
Output
3
Explanation
The subarrays of size 3 are [2,2,2], [2,2,2], [2,2,5], [2,5,5], [5,5,5], [5,5,8]. Among them, the subarrays with average >= 4 are [2,5,5], [5,5,5], and [5,5,8].