-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion4.java
More file actions
30 lines (27 loc) · 1019 Bytes
/
Copy pathQuestion4.java
File metadata and controls
30 lines (27 loc) · 1019 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
package practice4_sort;
import java.util.Arrays;
public class Question4 {
public int getAve(int[] score, int s, int e){
int sum = 0;
for(int i = s; i <= e; i++){
sum += score[i];
}
return (int)Math.floor((sum / (e - s + 1)));
}
public int solution(int[] score, int k){
int n = score.length;
Arrays.sort(score);
for(int i = 0; i <= n - k; i++){
if(score[i + k - 1] - score[i] <= 10)
return getAve(score, i, i + k - 1);
}
return 0;
}
public static void main(String[] args){
Question4 T = new Question4();
System.out.println(T.solution(new int[]{99, 97, 80, 91, 85, 95, 92}, 3));
System.out.println(T.solution(new int[]{92, 90, 77, 91, 70, 83, 89, 76, 95, 92}, 4));
System.out.println(T.solution(new int[]{77, 88, 78, 80, 78, 99, 98, 92, 93, 89}, 5));
System.out.println(T.solution(new int[]{88, 99, 91, 89, 90, 72, 75, 94, 95, 100}, 5));
}
}