Ctrl K

Bump Numbers to Fit the Divisors

Hard
You are given a list values and a short list divisors. In one step you can increase any number in values by 1. For every d in divisors, at least one number in values must be divisible by d. A single number may cover several divisors at once. Return the minimum number of steps needed.
Example 1
values=[7, 13], divisors=[3, 4] → 3

Raising 7 to 9 (divisible by 3) and 13 to 16 (by 4) costs 2 + 3 = 5. Raising 13 to 15 (by 3) and 7 to 8 (by 4) costs 2 + 1 = 3, which is better. Taking one number to 12, divisible by both, costs at least 5.

Example 2
values=[5, 9], divisors=[6, 3] → 1

Raising 5 to 6 is enough: 6 is divisible by both 6 and 3.

Example 3
values=[10, 1], divisors=[5] → 0
Constraints
  • 1 ≤ len(values) ≤ 5·10⁴
  • 1 ≤ len(divisors) ≤ 4, len(divisors) ≤ len(values)
  • 1 ≤ values[i], divisors[j] ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo Groups With the Same Average HardSquare-Sum Chains HardProduct of Distinct Primes HardFewest Lines Through the Dots MediumProducts Free of Squares Medium
loading Python…
Input
values=[7, 13], divisors=[3, 4]
Expected
3

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