-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathClosestNumbers.java
More file actions
27 lines (26 loc) · 703 Bytes
/
Copy pathClosestNumbers.java
File metadata and controls
27 lines (26 loc) · 703 Bytes
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
import java.io.*;
import java.util.*;
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<n;i++){
a[i] = sc.nextInt();
}
Arrays.sort(a);
String list="";
int min = Integer.MAX_VALUE;
for(int i=0;i<n-1;i++){
int diff = a[i+1] - a[i];
if(diff<min){
list = "";
min = diff;
list=a[i]+" "+a[i+1]+" ";
}
else if(diff==min)
list+=a[i]+" "+a[i+1]+" ";
}
System.out.println(list);
}
}