-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMaxAverageSubarrayCalculator.java
More file actions
29 lines (21 loc) · 972 Bytes
/
MaxAverageSubarrayCalculator.java
File metadata and controls
29 lines (21 loc) · 972 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
package sliding_window;
public class MaxAverageSubarrayCalculator {
public double findMaxAverage(int[] numbers, int subarrayLength) {
long currentSum = 0;
for (int i = 0; i < subarrayLength; i++) {
currentSum += numbers[i];
}
long maxSum = currentSum;
for (int i = subarrayLength; i < numbers.length; i++) {
currentSum += numbers[i] - numbers[i - subarrayLength];
maxSum = Math.max(maxSum, currentSum);
}
return maxSum / 1.0 / subarrayLength;
}
public static void main(String[] args) {
MaxAverageSubarrayCalculator calculator = new MaxAverageSubarrayCalculator();
assert Math.abs(calculator.findMaxAverage(new int[]{1,12,-5,-6,50,3}, 4) - 12.75) < 1e-5 : "Test case 1 failed";
assert Math.abs(calculator.findMaxAverage(new int[]{5}, 1) - 5.0) < 1e-5 : "Test case 2 failed";
System.out.println("All test cases passed!");
}
}