-
Notifications
You must be signed in to change notification settings - Fork 110
/
solution.java
50 lines (35 loc) · 1.43 KB
/
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
42
43
44
45
46
47
48
49
50
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//Taking input the number of elements in the array.
int n = Integer.parseInt(br.readLine());
String line = br.readLine();
String [] temp = line.split(" ");
int [] arr = new int[n];
//Taking input all the elements of an array.
for(int i = 0 ; i < n ; i++){
arr[i] = Integer.parseInt(temp[i]);
}
//To find the minimum difference between adjacent numbers , firstly the array is sorted.
Arrays.sort(arr);
int min_diff = Integer.MAX_VALUE;
//For loop to determine the minimum difference between adjacent elements.
for(int i=0;i<n-1;i++){
if(arr[i+1]-arr[i]<min_diff)
min_diff= arr[i+1]-arr[i];
}
//For loop to print the array elements with minimum difference .
for(int i=0;i<n-1;i++){
if(arr[i+1]-arr[i]==min_diff){
System.out.print(arr[i]+" "+arr[i+1]+" ");
}
}
}//Main() function ends.
}//Class ends.