Learn Partition with Minimum Difference with a step-by-step Dynamic Programming visualizer, JavaScript source code and a worked example. Practice subsequences and knapsack.
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.
Topic: Dynamic Programming · Subsequences and knapsack
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,
6,
11,
5
],
"k": 11,
"mode": "diff"
}
Execution steps
Include or exclude current value
Computed output
1
JavaScript reference implementation
const solve = ({
a,k,mode
},s)=>{
let d=Array(k+1).fill(0);
d[0]=1;
for(let x of a){
for(let j=k;
j>=x;
j--)d[j]+=d[j-x];
s("Include or exclude current value",{
Input:a,DP:d,Value:[x]
})
}
if(mode==="exists")return d[k]>0;
if(mode==="count")return d[k];
let j=k;
while(!d[j])j--;
return a.reduce((x,y)=>x+y,0)-2*j
};