-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumOfSubarrays.java
More file actions
47 lines (32 loc) · 791 Bytes
/
NumOfSubarrays.java
File metadata and controls
47 lines (32 loc) · 791 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.vinay.practice.lc;
// https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/description/
public class NumOfSubarrays {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = new int[] {2,2,2,2,5,5,5,8};
System.out.println(numOfSubarrays(arr, 3, 4));
}
public static int numOfSubarrays(int[] arr, int k, int threshold) {
int i = 0;
int avg = 0;
int sum = 0;
int count = 0;
while(i < k) {
sum = sum + arr[i];
i++;
}
avg = sum / k;
if(avg >= threshold) {
count++;
}
while(i < arr.length) {
sum += arr[i] - arr[i-k];
avg = sum / k;
if(avg >= threshold) {
count++;
}
i++;
}
return count;
}
}