-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinAvgTwoSlice.java
More file actions
32 lines (28 loc) · 995 Bytes
/
MinAvgTwoSlice.java
File metadata and controls
32 lines (28 loc) · 995 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
// Min Avg Two Slice: Find the starting slice in an array which has the lowest average.
// https://app.codility.com/programmers/lessons/5-prefix_sums/min_avg_two_slice/
class Solution {
public int solution(int[] A) {
double currentAvg = (A[0]+A[1])/2.0;
double minAvg = currentAvg;
int minAvgPos = 0;
for(int i=0; i<A.length-2; i++){
currentAvg = (A[i]+A[i+1])/2.0;
if(minAvg > currentAvg) {
minAvg = currentAvg;
minAvgPos = i;
}
currentAvg = (A[i]+A[i+1]+A[i+2])/3.0;
if(minAvg > currentAvg){
minAvg = currentAvg;
minAvgPos = i;
}
}
//check last two elements pair
currentAvg = (A[A.length-1]+A[A.length-2])/2.0;
if(minAvg > currentAvg){
minAvg = currentAvg;
minAvgPos = A.length-2;
}
return minAvgPos;
}
}