Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Minimum Deletions to Make Array Divisible LeetCode Solution

Problem – Minimum Deletions to Make Array Divisible LeetCode Solution

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.

Example 1:

Input: nums = [2,3,2,4,3], numsDivide = [9,6,9,3,15]
Output: 2
Explanation: 
The smallest element in [2,3,2,4,3] is 2, which does not divide all the elements of numsDivide.
We use 2 deletions to delete the elements in nums that are equal to 2 which makes nums = [3,4,3].
The smallest element in [3,4,3] is 3, which divides all the elements of numsDivide.
It can be shown that 2 is the minimum number of deletions needed.

Example 2:

Input: nums = [4,3,6], numsDivide = [8,2,6,10]
Output: -1
Explanation: 
We want the smallest element in nums to divide all the elements of numsDivide.
There is no way to delete elements from nums to allow this.

Constraints:

  • 1 <= nums.length, numsDivide.length <= 105
  • 1 <= nums[i], numsDivide[i] <= 109

Minimum Deletions to Make Array Divisible LeetCode Solution in Java

    public int minOperations(int[] A, int[] numsDivide) {
        int g = numsDivide[0], tmp;
        for (int a : numsDivide) {
            while (a > 0) { // g = gcd(g, a)
                tmp = g % a;
                g = a;
                a = tmp;
            }
        }
        Arrays.sort(A);
        for (int i = 0; i < A.length && A[i] <= g; ++i)
            if (g % A[i] == 0)
                return i;
        return -1;
    }

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

    int minOperations(vector<int>& A, vector<int>& numsDivide) {
        int g = numsDivide[0];
        for (int a: numsDivide)
            g = gcd(g, a);
        sort(A.begin(), A.end());
        for (int i = 0; i < A.size() && A[i] <= g; ++i)
            if (g % A[i] == 0)
                return i;
        return -1;
    }

Minimum Deletions to Make Array Divisible LeetCode Solution in Python

    def minOperations(self, A: List[int], numsDivide: List[int]) -> int:
        g = reduce(gcd, numsDivide)
        for i,a in enumerate(sorted(A)):
            if g % a == 0: return i
            if a > g: break
        return -1
Minimum Deletions to Make Array Divisible LeetCode Solution Review:

In our experience, we suggest you solve this Minimum Deletions to Make Array Divisible LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Minimum Deletions to Make Array Divisible LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Minimum Deletions to Make Array Divisible LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *