Skip to main content

Minimum Increments for Target Multiples in an Array - Solution & Explanation

Practice this problem

Problem Statement

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.

 

Example 1:

Input: nums = [1,2,3], target = [4]

Output: 1

Explanation:

The minimum number of operations required to satisfy the condition is 1.

  • Increment 3 to 4 with just one operation, making 4 a multiple of itself.

Example 2:

Input: nums = [8,4], target = [10,5]

Output: 2

Explanation:

The minimum number of operations required to satisfy the condition is 2.

  • Increment 8 to 10 with 2 operations, making 10 a multiple of both 5 and 10.

Example 3:

Input: nums = [7,9,10], target = [7]

Output: 0

Explanation:

Target 7 already has a multiple in nums, so no additional operations are needed.

 

Constraints:

  • 1 <= nums.length <= 5 * 104
  • 1 <= target.length <= 4
  • target.length <= nums.length
  • 1 <= nums[i], target[i] <= 104

Approach Overview

Problem Overview: You are given an array of numbers and a set of target values. You may increment any element any number of times. The goal is to ensure that for every target value, at least one array element becomes a multiple of it while minimizing the total increments performed.

Approach 1: Brute Force Assignment (Exponential Time, High Space)

The direct idea is to try assigning each array element to cover different target values. For every target, compute the cost to increment a number until it becomes a multiple of that target using (t - num % t) % t. Then explore all assignments of numbers to targets. This quickly becomes exponential because each element could serve multiple targets and every combination must be evaluated. Time complexity grows roughly O(n^m) with O(m) auxiliary space, making it impractical when the number of targets increases.

Approach 2: LCM + Bitmask Dynamic Programming (O(n · 2^m · m), Space O(2^m))

The key observation: a single number can satisfy multiple targets if it becomes a multiple of the LCM of those targets. Precompute the lcm for every subset of targets using bitmask representation. For each number in the array, calculate the cost to raise it to the next multiple of each subset LCM using (L - num % L) % L. Then run dynamic programming where dp[mask] represents the minimum cost to satisfy the targets represented by that mask.

Iterate through each array value and attempt to extend previously covered target sets. For every current mask and subset mask, update dp[newMask] where newMask = mask | subset. This works because making a number divisible by the LCM automatically makes it divisible by every target inside that subset. Precomputing subset LCMs keeps transitions fast and avoids repeated number theory calculations.

This transforms the problem into covering all targets (mask = (1<<m)-1) with minimal cost. The bitmask ensures each combination of satisfied targets is tracked efficiently. Since the number of targets is small, 2^m states remain manageable.

Recommended for interviews: The LCM + bitmask DP approach. Interviewers expect recognition that multiple targets can be satisfied simultaneously via LCM, followed by a DP over subsets. Mentioning the brute force shows understanding of the search space, but the bitmask optimization demonstrates strong algorithmic thinking.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Target AssignmentO(n^m)O(m)Conceptual baseline to understand cost of making numbers multiples of targets
LCM Precomputation + Bitmask DPO(n · 2^m · m)O(2^m)Optimal solution when number of targets is small and elements may satisfy multiple targets simultaneously

Video Solution

3444. Minimum Increments for Target Multiples in an Array | Bit Masking | LCM | HCF | DPAryan Mittal3,043 views views

Watch 6 more video solutions →

Frequently Asked Questions

Is Minimum Increments for Target Multiples in an Array easy or hard?
The problem is rated Hard because it combines number theory (LCM), subset enumeration, and bitmask dynamic programming. Recognizing that one number can satisfy multiple targets via LCM is the key insight that unlocks the optimal solution.
Minimum Increments for Target Multiples in an Array Python/Java solution
Most implementations compute LCM for every target subset, then run DP over bitmasks. For each number, calculate the increment needed to reach the next multiple of the subset LCM and update dp[newMask] = min(dp[newMask], dp[mask] + cost). The same logic works in Python, Java, C++, and Go.
How to solve Minimum Increments for Target Multiples in an Array in O(n)?
A strict O(n) solution is not typical because the problem requires evaluating combinations of target divisibility. The efficient approach uses bitmask dynamic programming with complexity O(n · 2^m · m), which is feasible because the number of targets is small.
What is the best approach for Minimum Increments for Target Multiples in an Array?
The optimal approach uses LCM precomputation combined with bitmask dynamic programming. Each bitmask represents a subset of targets already satisfied. For every array element, compute the cost to raise it to the next multiple of the LCM of a target subset and update DP states. This reduces the search space to O(n · 2^m · m).
Is Minimum Increments for Target Multiples in an Array asked at Google/Amazon/Meta?
Problems involving bitmask dynamic programming, LCM computation, and subset coverage appear frequently in interviews at companies like Google, Amazon, and Meta. Variants that combine number theory with DP are common in senior-level algorithm rounds.
What data structure is used in Minimum Increments for Target Multiples in an Array?
The main structure is a bitmask-based dynamic programming array where each index represents a subset of targets already satisfied. The solution also relies on LCM calculations from number theory and standard arrays for storing DP states.
What is the time complexity of Minimum Increments for Target Multiples in an Array?
The optimal solution runs in O(n · 2^m · m) time, where n is the array size and m is the number of target values. Each subset of targets is represented by a bitmask, and transitions are computed using precomputed LCM values.

Ready to solve this problem?

Practice Minimum Increments for Target Multiples in an Array with our built-in code editor and test cases.

Practice on FleetCode