Minimum Increments for Target Multiples in an Array - Problem

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

example_1.py โ€” Basic Case
$ Input: nums = [1, 4], target = [2, 3]
โ€บ Output: 2
๐Ÿ’ก Note: We can increment nums[0] from 1 to 2 (0 operations since 1โ†’2 via nums[1]=4โ†’4 covers target[0]=2), and increment nums[1] from 4 to 6 (2 operations) to get a multiple of 3. Total minimum: 2 operations.
example_2.py โ€” Multiple Options
$ Input: nums = [3, 1, 4], target = [2, 6]
โ€บ Output: 0
๐Ÿ’ก Note: nums already contains 4 which is a multiple of 2, and 6 which is a multiple of 6. No operations needed.
example_3.py โ€” Edge Case with Zero
$ Input: nums = [0, 5], target = [1, 0]
โ€บ Output: 1
๐Ÿ’ก Note: For target[0]=1, we need nums[0]=0โ†’1 (1 operation). For target[1]=0, nums[0]=0 already covers it (0 operations). Total: 1 operation.

Visualization

Tap to expand
๐ŸŽฏ Minimum Increments for Target MultiplesArrows (nums):1nums[0]4nums[1]Targets:2target[0]3target[1]Cost Analysis:Arrow 1 (value=1):โ†’ Target 2: need multiple โ‰ฅ1, so 1ร—2=2, cost = 2-1 = 1โ†’ Target 3: need multiple โ‰ฅ1, so 1ร—3=3, cost = 3-1 = 2Arrow 4 (value=4):โ†’ Target 2: need multiple โ‰ฅ4, so 2ร—2=4, cost = 4-4 = 0โ†’ Target 3: need multiple โ‰ฅ4, so 2ร—3=6, cost = 6-4 = 2Optimal Assignment:Arrow 4 covers Target 2 (cost=0) + Arrow 4 covers Target 3 (cost=2)Total Minimum Cost: 2 operations
Understanding the Visualization
1
Calculate Costs
For each arrow-target pair, calculate the minimum increments needed for the arrow to hit the target (reach a multiple)
2
State Exploration
Use bitmask DP to track which targets are covered and explore all possible assignments
3
Optimal Assignment
Find the assignment that covers all targets with minimum total cost
Key Takeaway
๐ŸŽฏ Key Insight: This is a minimum cost assignment problem where we need to cover all targets with minimum total increments. Bitmask DP efficiently explores all possible coverage combinations to find the optimal solution.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n ร— 2^m ร— m)

n elements ร— 2^m possible masks ร— m targets to check for each transition

n
2n
โœ“ Linear Growth
Space Complexity
O(2^m)

DP array of size 2^m to store minimum cost for each mask

n
2n
โœ“ Linear Space

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
Asked in
Google 45 Microsoft 38 Amazon 32 Meta 28
43.2K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen