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.

Example 1
Inputnums = [2,3,2,4,3], numsDivide = [9,6,9,3,15]
Output2
Deleting the two elements equal to 2 makes the smallest remaining element 3, which divides all elements of numsDivide, and 2 deletions is minimal.
Example 2
Inputnums = [4,3,6], numsDivide = [8,2,6,10]
Output-1
There is no way to delete elements from nums so that its smallest remaining element divides every element of numsDivide.

Constraints

  • 1 <= nums.length, numsDivide.length <= 10^5
  • 1 <= nums[i], numsDivide[i] <= 10^9

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate