1343.number-of-avg-subarr-sizek-greater-or-equal-threshold
Last updated
Was this helpful?
Last updated
Was this helpful?
This problem is pretty straigtforward, group each size k subarray, calculate avg of this group, compare avg and threshold.
Steps:
check whether current length of arr and size k
if len < k, return false. not enough elements to from a group subarray of size k.
scan arr from start position 0, each time check group a subarray of size k.
calculate avg of subarray of current size k [i, i + k -1],
if avg >= threshold, count + 1
otherwise, do nothing, continue, index i+1;
until last pos (len - k). return count.
For example:
Time Complexity: O(n) - n is the length of arr
Space Complexity: O(1) - no extra space
Group each size k subarray.
Calculate avg of subarray of size k, compare with threshold
Count increase 1 if meet requirements
Java Code