forked from tkggft/JavaClassicInterviewQuestions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain1.java
More file actions
40 lines (35 loc) · 966 Bytes
/
Main1.java
File metadata and controls
40 lines (35 loc) · 966 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
package ST;
import java.util.Scanner;
/**
* @auther: WJoe
* @Description:
* @Date : 19:33 2018/9/7
*/
public class Main1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int k = scanner.nextInt();
long[] ints = new long[n];
long sum = 0;
for (int i = 0; i < n; i++) {
ints[i] = scanner.nextLong();
sum += ints[i];
}
if (n == 0) {
System.out.println(0);
return;
}
double avg = (double)sum / n;
long max = Integer.MIN_VALUE;
long min = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (ints[i] > avg) {
ints[i] = ints[i] - k;
} else ints[i] = ints[i] + k;
max = Math.max(max, ints[i]);
min = Math.min(min, ints[i]);
}
System.out.println(max - min);
}
}