Algorithm for finding a group of numbers in a list that equal a target
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Finding a group of numbers that adds up to a target is a classic search problem with several different versions. Sometimes you need any one valid group, sometimes all groups, and sometimes numbers can only be used once. The right algorithm depends on those rules, because brute force, backtracking, and dynamic programming solve different variants well.
Clarify the Problem Variant
Before choosing an algorithm, answer these questions:
- Do you need one valid group or all valid groups.
- Can each number be used once or many times.
- Are negative numbers allowed.
- Is input size small enough for exponential search.
Those details completely change the best solution. A subset-sum style problem with positive integers and one-use elements is very different from an unbounded combination problem.
Backtracking for One Valid Group
If the input is moderate in size and each number can be used at most once, backtracking is a practical starting point.
This returns one valid combination such as [4, 5].
Backtracking for All Unique Groups
If you need every valid combination, keep exploring instead of stopping at the first solution.
Sorting plus duplicate skipping prevents repeated equivalent results.
Dynamic Programming for Reachability
If you only need to know whether a target can be reached, dynamic programming is often more efficient than exploring every subset.
This works well when numbers are non-negative and the target is not extremely large.
Recover an Actual Group With Dynamic Programming
You can extend DP to reconstruct one solution, not just a boolean answer.
This is useful when the target is reasonably bounded and you need one concrete answer.
Complexity Tradeoffs
Backtracking can degrade to exponential time in the worst case, but it is flexible and easy to adapt when you need actual combinations. Dynamic programming can be much faster for moderate targets, but it depends on target size and usually assumes non-negative values.
Rule of thumb:
- small list, need full combinations: backtracking
- moderate target, need feasibility or one example: DP
- very large input or unrestricted negatives: first clarify whether the problem needs approximation or extra constraints
Pruning Makes a Big Difference
For positive sorted numbers, you can stop early once a value exceeds the remaining target.
That simple rule removes large parts of the search tree and often turns a slow brute force attempt into an acceptable solution for interview-size inputs.
Common Pitfalls
- Starting without defining whether numbers can be reused. Fix: write the exact problem contract first.
- Returning duplicate combinations when input contains repeated numbers. Fix: sort input and skip equal values at the same recursion depth.
- Using DP when the target is too large to store efficiently. Fix: check target size before choosing a table-based approach.
- Assuming negative numbers fit the same pruning logic. Fix: revisit the algorithm if negatives are allowed.
- Optimizing before deciding whether you need one group or all groups. Fix: choose the algorithm around the output requirement.
Summary
- Target-sum problems have multiple variants, not one universal algorithm.
- Backtracking is a good default when you need actual combinations.
- Dynamic programming is strong when target size is moderate and inputs are non-negative.
- Sorting and pruning can drastically improve search performance.
- Define reuse, duplicates, and output requirements before implementation.

