forked from rajatgoyal715/Hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClosestNumbers.java
More file actions
33 lines (30 loc) · 764 Bytes
/
ClosestNumbers.java
File metadata and controls
33 lines (30 loc) · 764 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
28
29
30
31
32
33
package Sorting;
import java.util.*;
import java.io.*;
/*
* @author -- rajatgoyal715
*/
public class ClosestNumbers {
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);
}
}