Minimum Deletions to Make Array Divisible LeetCode solution in C++

1
class Solution {
2
public:
3
int minOperations(vector<int> &nums, vector<int> &numsDivide) {
4
int c = 0; // count
5
priority_queue<int, vector<int>, greater<int>> pq; /// min heap
6
int op = numsDivide[0];
7
int n = numsDivide.size();
8
for (int i = 1; i < n; i++) {
9
op = __gcd(op, numsDivide[i]); // as it will be the maximum number that
10
// will divide all the numbers
11
}
12
for (auto &i : nums) {
13
pq.push(i); // pushing all elements in the heap
14
}
15
while (!pq.empty()) {
16
int temp = pq.top();
17
pq.pop();
18
if (op % temp == 0) { // if the minimum element divides the gcd return
19
// count at that time
20
return c;
21
}
22
if (temp > op) { // if pq top become bigger than the find gcd then it returns -1
23
return -1;
24
}
25
c++;
26
}
27
return -1; // means the heap got empty and no element divide the numsDivide
28
// array
29
}
30
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0