Ctrl K

Smallest Divides Them All

Hard
You are given two arrays of positive integers, values and targets. You may remove any number of elements from values. The goal is for the smallest remaining element of values to divide every number in targets evenly. At least one element must remain in values. Return the minimum number of removals, or -1 if the goal cannot be reached.
Example 1
values=[8, 6, 4, 12, 6], targets=[18, 30, 12] → 1

The smallest value 4 does not divide 18 or 30. After removing it, the smallest value is 6, which divides 18, 30 and 12. One removal.

Example 2
values=[5, 10], targets=[7, 14] → -1

Neither 5 nor 10 divides 7, so it cannot be done.

Example 3
values=[3, 1, 3], targets=[100] → 0
Constraints
  • 1 ≤ len(values), len(targets) ≤ 10⁵
  • 1 ≤ values[i], targets[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSorting With Shared-Divisor Swaps HardNearest Points to the Base MediumPrices That Fade With Each Sale MediumSame Prizes, Different Tastes MediumThree Heaviest Diamonds Medium
loading Python…
Input
values=[8, 6, 4, 12, 6], targets=[18, 30, 12]
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 160 tests on the server, hidden ones included.