You're given two arrays: nums and target. Your mission is to make sure that every element in the target array has at least one multiple present in the nums array.
In each operation, you can increment any element in nums by 1. The challenge is to find the minimum number of operations needed to achieve this goal.
Example: If target = [2, 3] and nums = [1, 4], you need to increment 1 to 2 (1 operation) to get a multiple of 2, and increment 4 to 6 (2 operations) to get a multiple of 3. Total: 3 operations.
This problem combines number theory with optimization - you need to smartly assign elements from nums to target elements to minimize the total cost.
Input & Output
Visualization
Time & Space Complexity
n elements ร 2^m possible masks ร m targets to check for each transition
DP array of size 2^m to store minimum cost for each mask
Constraints
- 1 โค nums.length โค 10
- 1 โค target.length โค 10
- 0 โค nums[i], target[i] โค 104
- Each target element must have at least one multiple in nums after operations