Subset sum: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Process subtotals backward so each item can be used at most once.

Track totals reachable after each item; descending updates ensure each input item contributes at most once.

Open the 3D visualization

Subset sum example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Subset sum

Exact-target feasibility with nonnegative integers.

Choosing an alternative

Knapsack maximizes value rather than testing an exact sum.

Worked example: input and state changes

Example input

3,7,9 | 10

These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.

  1. Step 0: Initial state.

    State at this checkpoint
    {
      "values": [
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0
      ],
      "active": []
    }
  2. Step 4: Item 0 (3): subtotal 8 is unreachable.

    State at this checkpoint
    {
      "values": [
        1,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0
      ],
      "active": [
        8
      ]
    }
  3. Step 8: Item 0 (3): subtotal 4 is unreachable.

    State at this checkpoint
    {
      "values": [
        1,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0
      ],
      "active": [
        4
      ]
    }
  4. Step 12: Item 1 (7): subtotal 8 is unreachable.

    State at this checkpoint
    {
      "values": [
        1,
        0,
        0,
        1,
        0,
        0,
        0,
        0,
        0,
        0,
        1
      ],
      "active": [
        8
      ]
    }
  5. Step 16: Target 10 is reachable using each item at most once.

    State at this checkpoint
    {
      "values": [
        1,
        0,
        0,
        1,
        0,
        0,
        0,
        1,
        0,
        1,
        1
      ],
      "active": [
        10
      ]
    }

Expected final state

Target 10 is reachable using each item at most once.

Complexity and input bounds

Family: Dynamic programming. Time complexity: O(items × target).

Space complexity: O(target) reachable-total flags.

Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.

Items (1–16 entries, 0–99) | target (0–40).

The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.

Common mistakes and limitations

Ascending updates may accidentally reuse the current item; target size controls memory.

Further reading

For background on algorithm analysis and the JavaScript function interface:

Editorial and testing approach · Browse all algorithm guides

Complete JavaScript implementation

javascript
1
2
3
4
5
6
7
8
// Nonnegative items, n ≤ 16 and target ≤ 40. Returns 1 if a subset reaches target, else 0. Each item is used at most once. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation.
function algorithm(a, n, target) {
    let dp = Array(256).fill(0); dp[0] = 1;
    for (let i = 0; i < n; i++) {
        for (let s = target; s >= a[i]; s--) { if (dp[s - a[i]] == 1) { dp[s] = 1; } }
    }
    return dp[target];
}

Source

Foundational dynamic-programming paper. It establishes the general method, not the first publication or exact recurrence of this particular teaching example.