-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBetweenTwoSets.java
More file actions
76 lines (53 loc) · 1.61 KB
/
Copy pathBetweenTwoSets.java
File metadata and controls
76 lines (53 loc) · 1.61 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package rank;
import java.util.ArrayList;
import java.util.List;
/**
* created by @author suraj on 27/10/19
*/
public class BetweenTwoSets {
public static int getTotalX(List<Integer> a, List<Integer> b) {
// Write your code here
a.sort(Integer::compareTo);
b.sort(Integer::compareTo);
int hcf = hcfOfAllNumbers(b);
int lastElementOfA = a.get(a.size() - 1);
List<Integer> expectedPossibleList = new ArrayList<>();
for (int i = lastElementOfA; i <= hcf; i++) {
boolean isFactor = true;
for (int aElem : a) {
if (i % aElem != 0 || hcf % i != 0) {
isFactor = false;
break;
}
}
if (isFactor) {
expectedPossibleList.add(i);
}
}
return expectedPossibleList.size();
}
public static int hcfOfAllNumbers(List<Integer> arr) {
int hcf = arr.get(0);
for (int i = 1; i < arr.size(); i++) {
hcf = hcf(lowest(hcf, arr.get(i)), arr.get(i));
}
return hcf;
}
public static int lowest(int a, int b) {
return Math.min(a, b);
}
public static int hcf(int lowest, int highest) {
if (highest % lowest == 0) {
return lowest;
}
return hcf(highest % lowest, lowest);
}
public static void main(String[] args) {
List<Integer> a = new ArrayList<>();
a.add(1);
List<Integer> b = new ArrayList<>();
b.add(72);
b.add(48);
System.out.println(getTotalX(a, b));
}
}