Workspace Power Set
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Power Set visualizer

Learn Power Set with a step-by-step Bit Manipulation visualizer, JavaScript source code and a worked example. Practice bit operations.

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": false
}

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 rows
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Bit Manipulation · Bit operations

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": false
}

Execution steps

  1. Record subset

Computed output

[
  [],
  [
    1
  ],
  [
    1,
    2
  ],
  [
    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.