forked from charles-wangkai/hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
41 lines (34 loc) · 843 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
34
35
36
37
38
39
40
41
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] a = new int[N];
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
}
Arrays.sort(a);
int minDiff = Integer.MAX_VALUE;
List<Integer> indices = new ArrayList<Integer>();
for (int i = 0; i < a.length - 1; i++) {
int diff = a[i + 1] - a[i];
if (diff < minDiff) {
minDiff = diff;
indices.clear();
indices.add(i);
} else if (diff == minDiff) {
indices.add(i);
}
}
for (int i = 0; i < indices.size(); i++) {
if (i != 0) {
System.out.print(" ");
}
System.out.print(a[indices.get(i)] + " " + a[indices.get(i) + 1]);
}
sc.close();
}
}