BackmediumGraphsuncategorizedmedium

Minimum Elements for Target Sum Solution

Problem Statement

You are given an array of integers nums and an integer target. Your task is to find the minimum number of elements from nums that sum up exactly to target. Each element nums[i] can be included in the sum at most once. If no such combination of elements exists, return -1.

Example 1
Input
[1, 2, 3, 4, 5]
Output
2

Explanation: Step-by-step: with input [1, 2, 3, 4, 5] and target 7, we can select 2 and 5, giving output 2

Example 2
Input
[1, 1, 1, 1, 1]
Output
5

Explanation: Step-by-step: with input [1, 1, 1, 1, 1] and target 5, we can select all five 1s, giving output 5

Constraints

  • 1 <= nums.length <= 100
  • -1000 <= nums[i] <= 1000
  • -10^5 <= target <= 10^5
  • Each `nums[i]` can be used at most once.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Minimum Elements for Target Sum — Problem Statement & Solution Guide

GraphsMediumMixed
TimeO(n * target)
|
SpaceO(target)

Problem Description

You are given an array of integers nums and an integer target. Your task is to find the minimum number of elements from nums that sum up exactly to target. Each element nums[i] can be included in the sum at most once. If no such combination of elements exists, return -1.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Minimum Elements for Target Sum"

medium

WHY DOES IT MATTER?

Minimizing element count under a sum constraint appears in resource allocation, budgeting, and load‑balancing scenarios where you need to meet a quota with the fewest items to reduce overhead or cost.

OPTIMIZATION CHALLENGE

The breakthrough is recognizing that the problem can be expressed as a shortest‑path on a sum graph, allowing a DP that collapses exponential subsets into a linear‑size table by iterating sums in reverse order.

REAL-WORLD CONNECTION

Think of a shipping company that must fill a container to a precise weight using the fewest packages to minimize handling time; each package can be placed only once, mirroring the at‑most‑once constraint of the algorithm.

During an interview, code the DP first with a clear INF sentinel, iterate backwards, and early‑exit if dp[target] becomes 1 (the optimal lower bound). This shows both correctness and awareness of constant‑time shortcuts.

COMPLEXITY AT A GLANCE

⏱ Time:O(n * target)
💾 Space:O(target)

Core Theory — Why This Approach?

The problem is a constrained version of the classic Subset Sum: we must decide whether a subset of the given numbers can achieve an exact target, but we also need the subset with the smallest cardinality. A naïve recursive enumeration tries all 2^n subsets, which explodes even for moderate n (e.g., n=30 yields over a billion possibilities) and is therefore infeasible for typical interview constraints. The optimal paradigm leverages dynamic programming on the sum dimension: we maintain an array dp where dp[s] records the minimum number of elements required to reach sum s. By processing each number once and updating dp in reverse order (to enforce the “use‑at‑most‑once” rule), we transform the exponential search into a pseudo‑polynomial algorithm whose runtime grows linearly with n × target. This DP is essentially a shortest‑path problem on a DAG where each node represents a reachable sum and each edge adds a new element, and the shortest path length corresponds to the minimal element count.

When the target value is modest (≤10^5) the DP runs comfortably within time limits, but if target can be huge (e.g., up to 10^9) we must resort to alternative strategies such as meet‑in‑the‑middle, bitset optimizations, or pruning with greedy bounds. The key insight is that the state space can be reduced from exponential subsets to linear sums, and the reverse iteration guarantees each element contributes at most once, preserving correctness while achieving optimality.

Interview Questions on This Problem

Q1How would you modify the DP solution if each element could be used unlimited times (unbounded knapsack) while still minimizing the number of elements?

Switch the inner loop to iterate forward from num to target, allowing the same element to be considered multiple times. The transition becomes dp[s] = min(dp[s], dp[s‑num] + 1). This changes the problem to an unbounded knapsack where we seek the fewest coins to make change.

Q2Can you solve the problem in O(target) space without sacrificing time complexity? Explain the technique.

Yes. The DP array already uses O(target) space. By updating it in place and iterating numbers one by one, we never need a second dimension. The reverse iteration ensures each number is used at most once, so a single 1‑D array suffices.

Q3If the array size n is up to 40 but target can be as large as 10^12, which algorithmic approach would you choose and why?

Use a meet‑in‑the‑middle technique: split the array into two halves, enumerate all subset sums for each half (2^(n/2) ≈ 2^20 ≈ 1M), store for each sum the minimal count, then for each sum in the first half look for target‑sum in the second half using a hash map. This yields O(2^{n/2}) time and space, independent of the magnitude of target.

Examples

Example 1

Input

[1, 2, 3, 4, 5]

Output

2

Explanation: Step-by-step: with input [1, 2, 3, 4, 5] and target 7, we can select 2 and 5, giving output 2

Example 2

Input

[1, 1, 1, 1, 1]

Output

5

Explanation: Step-by-step: with input [1, 1, 1, 1, 1] and target 5, we can select all five 1s, giving output 5

Constraints

  • 1 <= nums.length <= 100
  • -1000 <= nums[i] <= 1000
  • -10^5 <= target <= 10^5
  • Each `nums[i]` can be used at most once.

Optimal Approach & Strategy

Use a 1‑D DP array where dp[s] holds the minimal element count to achieve sum s, updating it in reverse for each number to enforce single use, achieving O(n·target) time.

Brute Force Approach

Enumerate every subset of nums, compute its sum, and track the smallest subset size that equals target; this requires O(2^n) time.

Verified Code Solutions

JavaScript Solution
Time: O(n * target)
function minElementsForTargetSum(nums, target) {
      let dp = new Array(target + 1).fill(Infinity);
      dp[0] = 0;
      for (let num of nums) {
         for (let i = target; i >= num; i--) {
            dp[i] = Math.min(dp[i], dp[i - num] + 1);
         }
      }
      return dp[target] === Infinity ? -1 : dp[target];
   }

Asked in Top Tech Interviews

uncategorizedmediumnone

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.