forked from jhflorey/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClosestNumbers.java
executable file
·39 lines (33 loc) · 1023 Bytes
/
ClosestNumbers.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
package algorithm.sorting;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
/**
* Created by Jian on 11/02/2016.
*/
public class ClosestNumbers {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new FileInputStream("testcase.txt"));
int n = sc.nextInt();
long[] nums = new long[n];
for (int i = 0; i < n; i++) {
nums[i] = sc.nextLong();
}
Arrays.sort(nums);
long min = Integer.MAX_VALUE;
for (int i = 1; i < nums.length; i++) {
long value = nums[i] - nums[i - 1];
if (min > value) {
min = value;
}
}
for (int i = 1; i < nums.length; i++) {
long value = nums[i] - nums[i - 1];
if (value == min) {
System.out.print(nums[i - 1] + " " + nums[i] + " ");
}
}
}
}