[LeetCode] Minimum Deletions to Make Array Divisible

2344. Minimum Deletions to Make Array Divisible

You are given two positive integer arrays nums and numsDivide. You can delete any number of elements from nums.

Return the minimum number of deletions such that the smallest element in nums divides all the elements of numsDivide. If this is not possible, return -1.

Note that an integer x divides y if y % x == 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int minOperations(vector<int>& A, vector<int>& B) {
int g = B[0];
for(auto& b : B)
g = __gcd(g,b);
sort(begin(A), end(A));
for(int i = 0; i < A.size(); i++) {
if(A[i] > g) break;
if(g % A[i] == 0) return i;
}
return -1;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/07/18/PS/LeetCode/minimum-deletions-to-make-array-divisible/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.