Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold

Given an array of integers `arr` and two integers `k` and `threshold`, return the number of sub-arrays of size `k` whose average is greater than or equal to `threshold`.

30:00

Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
medium
Free written solution
Topics
Companies

Given an array of integers arr and two integers k and threshold, return the number of sub-arrays of size k whose average is greater than or equal to threshold.

Example 1:
Input: {"arr":[2,2,2,2,5,5,5,8],"k":3,"threshold":4}
Output: 3
Constraints:
  • 1≤arr.length≤1051 \leq \text{arr.length} \leq 10^5

  • 1≤arr[i]≤1041 \leq \text{arr}[i] \leq 10^4

  • 1≤k≤arr.length1 \leq k \leq \text{arr.length}

  • 0≤threshold≤1040 \leq \text{threshold} \leq 10^4

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"arr":[2,2,2,2,5,5,5,8],"k":3,"threshold":4}

Average >= 4 is the same as sum >= 3 * 4 = 12, so compare window sums against 12 and never divide.

arr, window of k = 3

2

2

2

2

5

5

5

8

0

1

2

3

4

5

6

7

- - threshold 4

window level

sum -

need 12

count = 0

Variables
VariableValue
k3
threshold4
target12
DepthFunction Call
Stack empty
0/10