Workspace Subset Sums
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Subset Sums visualizer

Learn Subset Sums with a step-by-step Recursion & Backtracking visualizer, JavaScript source code and a worked example. Practice subsets and combinations.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 5Execution step
Reference example · JavaScript
Inspect reference input
{
  "a": [
    1,
    2
  ],
  "unique": false,
  "sum": true
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
a,unique,sum
},s)=>{
a.sort((x,y)=>x-y);
let o=[];
function f(i,p){
o.push(sum?p.reduce((x,y)=>x+y,0):p);
s("Record subset",{
Input:a,Path:p,Output:o
});
for(let j=i;
j<a.length;
j++)if(!unique||j===i||a[j]!==a[j-1])f(j+1,[...p,a[j]])
}
f(0,[]);
return o
};

Execution canvas

Active Changed
STEP 1

Record subset

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Input2 items
Path0 items

Empty at this step.

Output1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Recursion & Backtracking · Subsets and combinations

Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.

Example input

{
  "a": [
    1,
    2
  ],
  "unique": false,
  "sum": true
}

Execution steps

  1. Record subset

Computed output

[
  0,
  1,
  3,
  2
]
JavaScript reference implementation
const solve = ({
  a,unique,sum
},s)=>{
  a.sort((x,y)=>x-y);
  let o=[];
  function f(i,p){
    o.push(sum?p.reduce((x,y)=>x+y,0):p);
    s("Record subset",{
      Input:a,Path:p,Output:o
    });
    for(let j=i;
    j<a.length;
    j++)if(!unique||j===i||a[j]!==a[j-1])f(j+1,[...p,a[j]])
  }
  f(0,[]);
  return o
};

Continue learning

Practice related DSA questions in the same topic.