-
Notifications
You must be signed in to change notification settings - Fork 31
/
MaxConsGap.java
76 lines (59 loc) · 1.73 KB
/
MaxConsGap.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
73
74
75
76
package Array;
import java.util.Arrays;
import java.util.List;
/**
* Author - archit.s
* Date - 01/09/18
* Time - 2:35 PM
*/
public class MaxConsGap {
public int maximumGap(final List<Integer> A) {
if(A.size()<2){
return 0;
}
int min = A.get(0);
int max = A.get(0);
for(int i=1;i<A.size();i++){
min = Math.min(min,A.get(i));
max = Math.max(max, A.get(i));
}
double gap = 0;
gap = max - min;
gap = gap/(A.size()-1);
if(gap == 0){
return max - min;
}
int[] maxArray = new int[A.size()];
int[] minArray = new int[A.size()];
Arrays.fill(minArray, Integer.MAX_VALUE);
Arrays.fill(maxArray, Integer.MIN_VALUE);
int bucket = 0;
for(int i=0;i<A.size();i++){
bucket = (int)((A.get(i)-min)/gap);
if(minArray[bucket] == Integer.MAX_VALUE){
minArray[bucket] = A.get(i);
}
else{
minArray[bucket] = Math.min(minArray[bucket], A.get(i));
}
if(maxArray[bucket] == Integer.MIN_VALUE){
maxArray[bucket] = A.get(i);
}
else{
maxArray[bucket] = Math.max(maxArray[bucket], A.get(i));
}
}
int result = 0;
int ind = 0;
for(int i=0;i<A.size();i++){
if(minArray[i] != Integer.MAX_VALUE){
result = Math.max(result, minArray[i] - maxArray[ind]);
ind = i;
}
}
return result;
}
public static void main(String[] args) {
System.out.println(new MaxConsGap().maximumGap(Arrays.asList(1,10,5)));
}
}