-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovingApples.java
More file actions
42 lines (32 loc) · 982 Bytes
/
Copy pathMovingApples.java
File metadata and controls
42 lines (32 loc) · 982 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
41
42
import java.util.Arrays;
public class MovingApples {
public int moveApples(int input1, int[] input2) {
if (input1 <= 1)
return 0;
if (input1 == 2) {
return Math.abs(input2[0] - input2[1]);
}
Arrays.sort(input2);
int ans = 0;
int sum = 0;
for (int i = 0; i < input1; i++) {
sum += input2[i];
}
int avg = sum / input1;
for (int i = 0; i < input1 - 1; i++) {
if (input2[i] > avg) {
ans += input2[i] - avg;
input2[i + 1] += input2[i] - avg;
}
if (input2[i] < avg) {
ans += avg - input2[i];
input2[i + 1] -= avg - input2[i];
}
}
return ans;
}
public static void main(String[] args) {
MovingApples obj = new MovingApples();
System.out.println(obj.moveApples(5, new int[]{2849, 1620, 705, 1, 30}));
}
}