Minimum Increments for Target Multiples in an Array - Problem
You are given two arrays, nums and target. In a single operation, you may increment any element of nums by 1.
Return the minimum number of operations required so that each element in target has at least one multiple in nums.
A multiple of a number x is any number that can be expressed as k * x where k is a positive integer.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,2,3], target = [2,4]
›
Output:
3
💡 Note:
Assign nums[0]=1 to target[0]=2: cost 2-1=1 to make 1→2. Assign nums[1]=2 to target[1]=4: cost 4-2=2 to make 2→4. Total cost: 1+2=3.
Example 2 — Zero Elements
$
Input:
nums = [0,0,0], target = [1,2,3]
›
Output:
6
💡 Note:
Transform 0→1 (cost 1), 0→2 (cost 2), 0→3 (cost 3). Total cost: 1+2+3=6.
Example 3 — Multiple Assignment
$
Input:
nums = [3,5], target = [2,4]
›
Output:
1
💡 Note:
Assign nums[0]=3 to target[1]=4: cost 4-3=1. Assign nums[1]=5 to target[0]=2: 5 is already multiple of 2 (5=2×2.5), but we need integer multiples, so 5→6 costs 1, then 6→8 costs 2, giving 6=2×3. Actually, 5→4 costs -1 which is impossible. We need 5→6→8→10→... The first multiple ≥5 is 6=2×3, cost 1. Wait, 5 to get multiple of 2: need k×2≥5, so k≥2.5, k=3, so 6. Cost=6-5=1. Total: assign nums[1]=5 to target[0]=2 costs 1, nums[0]=3 to target[1]=4 costs 1. Total=2. But optimal is nums[0]=3→target[0]=2 (cost 0, since 3 is not multiple of 2, need 4=2×2, cost 1), nums[1]=5→target[1]=4 (cost 0, since need 8=4×2, cost 3). Let me recalculate: 3→multiple of 2: need 4, cost 1. 5→multiple of 4: need 8, cost 3. Total 4. Alternative: 3→multiple of 4: need 4, cost 1. 5→multiple of 2: need 6, cost 1. Total 2. So minimum is 2.
Constraints
- 1 ≤ nums.length, target.length ≤ 8
- 1 ≤ nums[i], target[i] ≤ 100
Visualization
Tap to expand
Understanding the Visualization
1
Input Arrays
nums=[1,2,3] and target=[2,4]
2
Assignment
Find optimal assignment of nums to targets
3
Calculate Cost
Sum increment costs for each assignment
Key Takeaway
🎯 Key Insight: Find the minimum cost bipartite matching between nums and target elements
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code