-
Notifications
You must be signed in to change notification settings - Fork 5
/
MinimumDeletionsToMakeArrayDivisible.java
56 lines (48 loc) · 1.44 KB
/
MinimumDeletionsToMakeArrayDivisible.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
/*https://leetcode.com/problems/minimum-deletions-to-make-array-divisible/*/
class Solution {
public int minOperations(int[] nums, int[] numsDivide) {
int result = 0, key, val;
boolean flag = true, check;
TreeMap<Integer,Integer> map = new TreeMap<Integer,Integer>();
for (int num : nums)
map.put(num,map.getOrDefault(num,0)+1);
for (Map.Entry elem : map.entrySet())
{
key = (Integer)elem.getKey();
val = (Integer)elem.getValue();
check = true;
for (int div : numsDivide)
{
if (div < key || div%key != 0)
{
check = false;
break;
}
}
if (!check) result += val;
else return result;
}
if (!flag) return -1;
if (result == nums.length) return -1;
return result;
}
}
class Solution {
public int minOperations(int[] nums, int[] numsDivide) {
Arrays.sort(nums);
int gcd = numsDivide[0];
for (int num : numsDivide){
gcd = gcd(gcd, num);
}
for (int i = 0; i < nums.length; ++i){
if (gcd % nums[i] == 0)
return i;
}
return -1;
}
private int gcd(int a , int b){
if (a % b == 0)
return b;
return gcd(b, a % b);
}
}