Subset Sum Calculator

Find every subset of a list that adds to a target, with the DP table and closest sums.

  1. Enter valuesUse your own inputs or an example
  2. CalculateApply your selected settings
  3. View resultExplore the answer and explanation

Enter the set and the target

Enter your values below to get started.

Examples:
Up to 100 numbers ≥ 0, separated by commas, spaces or new lines. Up to 2 decimals (e.g. 19.99).
Use each number
Reset restores the default values.

Tips for this calculator

Duplicates count as separate items: {5, 5} has 2 subsets that sum to 5.

Pick Any number of times for coin-change style problems.

Prices work: 19.99 is handled in whole cents, so no rounding errors.

With a target ≤ 40 and ≤ 14 numbers, the DP table tab draws the whole table.

A subset sum calculator finds which numbers from a list add up exactly to a target. Enter up to 100 numbers (decimals to 2 places, such as prices) and a target to get the number of solutions, every matching subset up to your limit, the dynamic programming (DP) table that proves the answer, and the closest sums above and below when no exact subset exists.

What is the Subset Sum Problem?

The subset sum problem asks whether some subset of a list of numbers adds up exactly to a target value T, and if so, which subset. For the set {3, 34, 4, 12, 5, 2} and T = 9, 2 subsets work: {4, 5} and {3, 4, 2}.

Subset sum is one of Richard Karp's 21 NP-complete problems (1972): no known algorithm solves every case in time polynomial in the number of bits of the input. A set of n numbers has 2n subsets, so 40 numbers already give 1,099,511,627,776 subsets to check by brute force.

The problem is a special case of the 0/1 knapsack problem where each item's value equals its weight.

InteractivePick numbers to hit the target

How Does the Subset Sum Calculator Work?

The subset sum calculator fills a dynamic programming table of reachable sums, reads the answer from the cell for the full set and the target, then walks back through the table to list each subset. The method, from Richard Bellman's dynamic programming (1950s), takes n × T steps instead of 2n.

InteractiveFill the subset sum table row by row
  1. Scale to whole numbers

    Decimals are multiplied by 100 (cents) so the table has whole-number columns.

  2. Count the ways

    ways[s] counts how many subsets reach each sum s, item by item.

  3. Trace solutions

    A depth-first search guided by the table lists subsets with no dead ends, up to your limit.

  4. Find the closest sums

    With no exact match, the calculator reports the nearest reachable sum below and above T.

Subset Sum Recurrence Formula

The subset sum recurrence says a sum s is reachable with the first i items when it was reachable without item i, or when s − ai was reachable without item i. The base case is dp[0][0] = true.

dp[i][s]=dp[i−1][s]  ∨  dp[i−1][s−ai],dp[0][0]=truedp[i][s] = dp[i-1][s] \;\lor\; dp[i-1][s - a_i], \qquad dp[0][0] = \text{true}

Counting Formula

Replacing OR with + counts the subsets instead of testing them.

N[i][s]=N[i−1][s]+N[i−1][s−ai]N[i][s] = N[i-1][s] + N[i-1][s - a_i]

Unbounded (Repeats Allowed) Formula

When each number can be reused, the 2nd term looks in the same row. Coins {1, 2, 5} make 11 in 11 ways.

dp[i][s]=dp[i−1][s]  ∨  dp[i][s−ai]dp[i][s] = dp[i-1][s] \;\lor\; dp[i][s - a_i]
InteractivePick a cell: where does its value come from?

Types of Subset Sum Problems

There are 4 main types of subset sum problems: the decision version, the counting version, the unbounded version and the partition problem. All 4 use the same table with a different question asked of it.

InteractiveOne set, 4 versions of subset sum

Decision subset sum

Is there any subset that adds to T? Answer: yes or no. {1, 5, 6, 9, 11} → 15: yes (6 + 9).

Counting subset sum

How many subsets add to T? {1, 5, 6, 9, 11} has 2 subsets that sum to 15: {6, 9} and {1, 5, 9}.

Unbounded subset sum

Each number can repeat, as with coins. {1, 5, 6, 9, 11} reaches 15 in 11 ways.

Partition problem

Can the set split into 2 halves with equal sums? {1, 5, 6, 9, 11} totals 32: {5, 11} and {1, 6, 9} both sum to 16.

How to Solve a Subset Sum by Hand

To solve a subset sum by hand, keep a list of reachable sums, start with {0}, and for each number add it to every sum already on the list; the target is reachable when it appears. Here is {2, 3, 7, 8, 10} with T = 11:

  1. Start: {0}.
  2. Add 2: {0, 2}.
  3. Add 3: {0, 2, 3, 5}.
  4. Add 7: {0, 2, 3, 5, 7, 9, 10} (12 is over 11, drop it).
  5. Add 8: 3 + 8 = 11 appears, so the answer is yes: {3, 8}.
InteractiveStep through subset sums by hand

    Subset Sum Complexity: NP-Complete but Pseudo-Polynomial

    Subset sum is NP-complete, yet the dynamic programming solution runs in O(n·T) time, which is fast whenever the target T is small. This is called pseudo-polynomial time: the cost grows with the size of T itself, not with the number of digits in T.

    brute force: O(2n⋅n)DP: O(n⋅T)meet in the middle: O(2n/2⋅n)\text{brute force: } O(2^n \cdot n) \qquad \text{DP: } O(n \cdot T) \qquad \text{meet in the middle: } O(2^{n/2} \cdot n)

    With 30 numbers and T = 10,000, the table has 300,000 cells, while brute force checks 230 ≈ 1.07 billion subsets. With T = 1015, the table is too large and Horowitz–Sahni's meet-in-the-middle method (1974) is the better choice.

    Interactive2ⁿ brute force vs. n·T dynamic programming

    Common Mistakes with Subset Sums

    • Reusing a number in the 0/1 version: in classic subset sum each item is used at most once, so {5} cannot make 10.
    • Filling the 0/1 table left to right in one row: updating sums upward in place reuses the current item; loop s from T down to ai.
    • Ignoring duplicates: equal values are separate items, so {5, 5, 10} has 2 subsets that sum to 10: {5, 5} and {10}.
    • Rounding decimals: 0.1 + 0.2 is not exactly 0.3 in floating point; work in cents.
    • Brute-forcing large sets: 2n grows past 1 trillion at n = 40.
    InteractiveTrue or false: subset sums
    1. The empty subset always sums to 0.

    2. A set of 20 numbers has 400 subsets.

    3. Subset sum is NP-complete.

    4. The DP method is fast for any target, however large.

    5. {6, 10, 15} has a subset that sums to 16.

    Score: 0 / 5

    How to Use the Subset Sum Calculator

    To use the subset sum calculator, enter the set, the target, how many solutions to list and whether numbers can repeat, then press Calculate. The result card shows how many solutions exist and the first one; the tabs show the steps, every listed subset and the DP table.

    InteractiveLoad a subset sum problem

    Each button fills the calculator and runs it.

    1. Enter the set

      Up to 100 non-negative numbers; decimals up to 2 places.

    2. Enter the target

      The sum you want, for example 9 or 25.24.

    3. Choose the limit

      List 1, 10, 50, 200 or 500 solutions. The count always covers all of them.

    4. Pick once or repeats

      Once for classic subset sum, any number of times for coin change.

    Real-Life Uses of Subset Sums

    Subset sums answer any question of the form "which of these amounts add up to that total?" There are 5 common uses:

    • Accounts reconciliation: find which open invoices make up a single bank payment of $53.00.
    • Budget shopping: pick items whose prices use a $250 budget (about €230) as fully as possible.
    • Load balancing: the partition problem splits tasks between 2 servers with equal total run time.
    • Coin change: the unbounded version counts the ways to make 11 cents from 1-, 2- and 5-cent coins (11 ways).
    • Cryptography: the Merkle–Hellman knapsack cryptosystem (1978) built a public-key cipher on subset sum.
    InteractiveSpend a budget as closely as possible

    Subset Sum Examples

    Classic subset sum problems and their answers.

    Subset Sum Examples
    SetTargetAnswerCalculator
    {3, 34, 4, 12, 5, 2}92 subsets: {4, 5}, {3, 4, 2}
    {2, 3, 7, 8, 10}111 subset: {3, 8}
    {1, 2, …, 10}1520 subsets
    {10, 20, 30, 40, 50}1003 subsets
    {19.99, 5.49, 12.50, 7.25, 3.75}25.24{5.49, 12.50, 7.25}
    {1, 2, 5}, repeats1111 combinations
    {6, 10, 15}7None (closest 6 and 10)

    Popular Subset Sum Problems

    Try each one, then reveal the answer or solve it in the calculator.

    1. Which subsets of {15, 22, 14, 26, 32, 9, 16, 8} add up to 53?

      Show answer3 subsets: {15, 22, 16}, {22, 14, 9, 8}, {15, 14, 16, 8}
    2. Does any subset of {2, 4, 6, 8} sum to 9?

      Show answerNo: every sum is even. Closest: 8 and 10.
    3. How many subsets of {1, 3, 5, 7, 9} sum to 16?

      Show answer2: {7, 9} and {1, 3, 5, 7}
    4. Can {1, 5, 6, 9, 11} be split into 2 groups with equal sums?

      Show answerYes: {5, 11} and {1, 6, 9}, both 16
    5. In how many ways can 1-, 2- and 5-cent coins make 11 cents?

      Show answer11 ways
    6. What is the closest sum to 30 using {3, 34, 4, 12, 5, 2} once each?

      Show answerNo exact subset; 26 below and 34 above

    Explore More Sum Calculators

    Discover 67 free sum calculators across different categories. Find the right calculator for your needs.

    Browse All 67 Calculators
    Questions and answers

    Frequently Asked Questions

    What is a subset sum calculator?

    A subset sum calculator is an online tool that finds which numbers from a list add up to a target sum. This subset sum calculator counts every solution, lists up to 500 of them, draws the dynamic programming table and gives the closest sums when no exact subset exists.

    How do you solve the subset sum problem?

    Solve the subset sum problem with dynamic programming: keep a table of reachable sums, start from sum 0, and for each number mark every old sum plus that number as reachable. The target is solvable when its cell is marked.

    Is subset sum NP-complete?

    Yes, subset sum is NP-complete. Richard Karp included it among 21 NP-complete problems in 1972, but the O(n·T) dynamic programming method still solves it fast when the target T is small.

    Can the subset sum calculator use decimals?

    Yes, the subset sum calculator uses decimals with up to 2 places. It multiplies every value by 100 and works in whole cents, so 19.99 + 5.25 is exact.

    Can a number be used more than once?

    No, not in the classic subset sum problem: each number is used at most once. Pick Any number of times for the unbounded version, as in coin change.

    What happens if no subset matches the target?

    The subset sum calculator reports no exact solution and shows the closest reachable sums below and above the target, such as 6 and 10 for the set {6, 10, 15} and target 7.

    How many subsets does a set of n numbers have?

    A set of n numbers has 2ⁿ subsets, including the empty set. 10 numbers have 1,024 subsets and 30 numbers have 1,073,741,824.

    What is the difference between subset sum and knapsack?

    Subset sum asks for a total exactly equal to T, and 0/1 knapsack maximizes value under a weight limit. Subset sum is the knapsack case where each item's value equals its weight.

    Conclusion

    The subset sum problem asks which numbers from a list add to a target, and the subset sum calculator answers it with a dynamic programming table, a full count of solutions and a list of subsets. Use the 0/1 mode for invoices and budgets, the unbounded mode for coins, and the closest sums when no exact match exists.

    Not sure which version of the problem you have? Answer the questions below.

    InteractiveWhich subset sum setting do you need?