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:
1
💡 Note:
Optimal assignment: nums[1]=2 to target[0]=2 (cost 0, since 2 is already a multiple of 2), nums[2]=3 to target[1]=4 (cost 1, since we need 4 which is 3+1). Total cost: 0+1=1.
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:
2
💡 Note:
Optimal assignment: nums[0]=3 to target[1]=4 (cost 1, since we need 4 which is 3+1), nums[1]=5 to target[0]=2 (cost 1, since we need 6 which is the smallest multiple of 2 that's ≥5, costing 6-5=1). Total cost: 1+1=2.
Constraints
- 1 ≤ nums.length, target.length ≤ 8
- 1 ≤ nums[i], target[i] ≤ 100
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code