Files

36 lines
1017 B
Java
Raw Permalink Normal View History

2017-03-24 00:02:30 -05:00
// Author: Rodney Shaghoulian
2018-07-27 16:45:56 -07:00
// Github: github.com/RodneyShag
// HackerRank: hackerrank.com/RodneyShag
2017-03-24 00:02:30 -05:00
import java.util.Scanner;
import java.util.Arrays;
2017-04-07 20:17:38 -05:00
// 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
2017-03-24 00:02:30 -05:00
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;
}
}