-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmallestInterval.java
72 lines (54 loc) · 1.98 KB
/
SmallestInterval.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package tomkous.algos;
public class SmallestInterval {
public static void main(String a[]) {
SmallestInterval si = new SmallestInterval();
int[] numbers1 = {0,1,4,5,6,7};
int[] numbers2 = {2,4,5,6,7};
int[] numbers3 = {4,6,7};
int[] numbers4 = {4,5,7,4};
int[] numbers5 = {1,0,5,3};
System.out.print("Smallest interval between two numbers of ");
si.printArrayElements(numbers1);
System.out.println(" is " + si.findSmallestInterval(numbers1));
System.out.print("Smallest interval between two numbers of ");
si.printArrayElements(numbers2);
System.out.println(" is " + si.findSmallestInterval(numbers2));
System.out.print("Smallest interval between two numbers of ");
si.printArrayElements(numbers3);
System.out.println(" is " + si.findSmallestInterval(numbers3));
System.out.print("Smallest interval between two numbers of ");
si.printArrayElements(numbers4);
System.out.println(" is " + si.findSmallestInterval(numbers4));
System.out.print("Smallest interval between two numbers of ");
si.printArrayElements(numbers5);
System.out.println(" is " + si.findSmallestInterval(numbers5));
}
public void printArrayElements(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
System.out.print( a[i] + ",");
}
System.out.print( a.length );
}
public int findSmallestInterval(int[] a) {
/*
Please implement this method to
return the number that is the smallest interval
between any two of array integer elements.
*/
int result = Math.abs(a[0] - a[1]);
int temp = 0;
for(int i = 0; i < a.length-1; i++){
for(int j = i + 1; j < a.length; j++) {
temp = Math.abs(a[i] - a[j]);
if (temp < result)
result = temp;
}
}
/*
* for(int j = 0; j < a.length; j++) {
*
* if (Math.abs(a[j]) < result) result = Math.abs(a[j]); }
*/
return result;
}
}