-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Solution.java
33 lines (27 loc) · 930 Bytes
/
Solution.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
// github.com/RodneyShag
import java.util.Scanner;
import java.util.Arrays;
// Algorithm: Sort the array and compare adjacent pairs
// Greedy algorithm? No
// A greedy algorithm makes "the locally optimal choice at each
// stage with the hope of finding a global optimum" - Wikipedia
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int [] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = scan.nextInt();
}
scan.close();
System.out.println(minAbsoluteDifference(array));
}
private static int minAbsoluteDifference(int [] array) {
Arrays.sort(array);
int min = Integer.MAX_VALUE;
for (int i = 1; i < array.length; i++) {
min = Math.min(min, Math.abs(array[i] - array[i-1]));
}
return min;
}
}