-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAverageOfSubarrayOfSizeK.java
More file actions
22 lines (21 loc) · 940 Bytes
/
AverageOfSubarrayOfSizeK.java
File metadata and controls
22 lines (21 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Arrays;
public class AverageOfSubarrayOfSizeK {
public static double[] findAverages(int K, int[] arr) {
double[] result = new double[arr.length - K + 1];
int windowSum = 0;
int windowStart = 0;
for (int windowEnd = 0; windowEnd < arr.length; windowEnd++) {
windowSum += arr[windowEnd];
if (windowEnd >= K - 1) {
result[windowStart] = (double)windowSum / K; //calculate the average for the current window
windowSum -= arr[windowStart]; //subtract the element going out
windowStart += 1; //slide window ahead
}
}
return result;
}
public static void main(String[] args) {
double[] result = AverageOfSubarrayOfSizeK.findAverages(5, new int[] { 1, 3, 2, 6, -1, 4, 1, 8, 2 });
System.out.println("Averages of subarrays of size K: " + Arrays.toString(result));
}
}