forked from theprogrammedwords/Algorithm-Solutions-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinDiff.java
More file actions
56 lines (41 loc) · 1.05 KB
/
MinDiff.java
File metadata and controls
56 lines (41 loc) · 1.05 KB
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
51
52
53
54
55
56
/*Problem Description
Find the minimum difference possible between any two elements in the given array.
Input format
First line will contain a single integer n representing the size of the array.
Second line will contain n space separated integers representing the array.
Output format
Output the answer in single line.
Sample Input 1
3
1 2 4
Sample Output 1
1
Explanation 1
2 - 1 = 1 minimum difference
Constraints
2<=n<=100000
1<=A[i]<=1000000000
*/
import java.util.*;
class MinDiff {
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();
}
int res = minDiff(n, a);
System.out.println(res);
}
static int minDiff(int n, int a[]) {
int mini = Integer.MAX_VALUE;
Arrays.sort(a);
for(int i=0; i<a.length-1; i++){
if(mini > a[i+1]- a[i]){
mini = a[i+1]- a[i];
}
}
return mini;
}
}