-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetLargestGap.java
More file actions
59 lines (45 loc) · 1.19 KB
/
Copy pathGetLargestGap.java
File metadata and controls
59 lines (45 loc) · 1.19 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
57
58
59
//Write a function int solution(int A[], int N) that returns largest possbible gap size
import java.util.Arrays;
class GetLargestGap{
public static void main(String[] arg){
int N = 8 ;
int[] A = {10,0,8,2,-1,12,11,3};
System.out.println(solution(A,N));
}
static int solution(int[] A, int N){
//Sort Array
int[] B = A;
Arrays.sort(B);
System.out.println(Arrays.toString(B));
// int smallest = B[0];
// System.out.println(smallest);
// int largest = B[N-1];
// System.out.println(largest);
int max_gap = 0;
int gap = 0;
int result = 0;
for(int i=0;i<=N-2;i++){
// for(int j=B[i];j<=B[i+1];j++){
// int min = Math.min(j-B[i],B[i+1]-j);
// System.out.println("min "+min);
// System.out.println("j "+j);
// if(min>gap){
// gap = min;
// }
// }
gap = Math.abs(B[i]-B[i+1]);
if(gap>max_gap){
// System.out.println("----");
// System.out.println(B[i]);
// System.out.println(B[i+1]);
System.out.println(Math.abs(gap/2));
int Y = Math.min(B[i],B[i+1])+Math.abs(gap/2);
// System.out.println(Y);
// System.out.println("----");
max_gap = gap;
result = Math.abs(gap/2);
}
}
return result;
}
}