forked from ravipatel447/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathequal_beauty.java
81 lines (61 loc) · 2.56 KB
/
equal_beauty.java
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.*;
public class equal_beauty {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long ar[] = new long [n];
for(int i=0;i<n;i++){
ar[i] = sc.nextLong();
}
Arrays.sort(ar);
long final_ans = Long.MAX_VALUE;
if(n==2){
final_ans = 0;
}
else if(n==3){
long a = Math.abs(ar[0]-ar[1]);
long b = Math.abs(ar[1]-ar[2]);
long c = Math.abs(ar[0]-ar[2]);
final_ans = Math.min(a,Math.min(b, c));
}
else{
{
for(int i=1;i<n-1;i++){
for(int j=1;j<n-1;j++){
if(i!=j){
long t_ans = Math.abs((ar[i]-ar[0])-(ar[n-1]-ar[j]));
if(final_ans>t_ans){
final_ans = t_ans;
}
}
}
}
}
// case 2
for(int i=0;i<n-1;i++){
long t_ans = Math.abs(((ar[i])-(ar[0])) - ((ar[n-1])-ar[i+1]));
if(final_ans>t_ans){
final_ans = t_ans;
}
}
}
System.out.println(final_ans);
// long beauty = ar[n-1]-ar[0];
// long beauty_mode = Math.abs(beauty);
// long min = Long.MAX_VALUE;
// for(int i=1;i<n-2;i++){
// for(int j=i+1;j<n-1;j++){
// long current = Math.abs(ar[i]-ar[j]);
// long diff = Math.abs(current-beauty_mode);
// if(min>diff){
// min = diff;
// }
// }
// }
// if(n-2<=1){
// min = beauty_mode;
// }
// System.out.println(min);
sc.close();
}
}