forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimiseAbsDiff.java
More file actions
47 lines (37 loc) · 1.22 KB
/
MinimiseAbsDiff.java
File metadata and controls
47 lines (37 loc) · 1.22 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
package TwoPointers;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Author - archit.s
* Date - 15/10/18
* Time - 11:56 AM
*/
public class MinimiseAbsDiff {
public int solve(ArrayList<Integer> A, ArrayList<Integer> B, ArrayList<Integer> C) {
int i=0;
int j=0;
int k=0;
int min_diff = Math.abs(Math.max(A.get(i), Math.max(B.get(j), C.get(k))) - Math.min(A.get(i), Math.min(B.get(j), C.get(k))));
while(i<A.size() && j<B.size() && k<C.size()){
int max = Math.max(A.get(i), Math.max(B.get(j), C.get(k)));
int min = Math.min(A.get(i), Math.min(B.get(j), C.get(k)));
int current = Math.abs(max - min);
if(current < min_diff){
min_diff = current;
}
if(min == A.get(i)){
i++;
}
else if(min == B.get(j)){
j++;
}
else{
k++;
}
}
return min_diff;
}
public static void main(String[] args) {
System.out.println(new MinimiseAbsDiff().solve(new ArrayList<>(Arrays.asList(1,4,10)), new ArrayList<>(Arrays.asList(2,15,20)), new ArrayList<>(Arrays.asList(10,12))));
}
}