Back to questionsDSA Visualizers: 388 Coding Questions Target Sum visualizer Learn Target Sum with a step-by-step Dynamic Programming visualizer, JavaScript source code and a worked example. Practice subsequences and knapsack.
INTERACTIVE EXECUTION STUDIOLEARN BY DOING Predict. Execute. Understand. Follow the source code, inspect live state changes, and predict the next operation before revealing it.
1 / 6 Execution step
Prediction modePanel width Reference example · JavaScript
Inspect reference input {
"a": [
1,
1,
1,
1,
1
],
"k": 4,
"mode": "count"
} Code Visuals
JavaScript reference implementation · highlighted trace statement
1 const solve = ({
2 a,k,mode
3 },s)=>{
4 let d=Array(k+1 ).fill(0 );
5 d[0 ]=1 ;
6 for (let x of a){
7 for (let j=k;
8 j>=x;
9 j--)d[j]+=d[j-x];
10 s("Include or exclude current value",{
11 Input:a,DP:d,Value:[x]
12 })
13 }
14 if (mode==="exists")return d[k]>0 ;
15 if (mode==="count")return d[k];
16 let j=k;
17 while (!d[j])j--;
18 return a.reduce((x,y)=>x+y,0 )-2 *j
19 };
Execution canvas Active ChangedSTEP 1 Include or exclude current value Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.
Worked example and algorithm walkthrough 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,
1,
1,
1,
1
],
"k": 4,
"mode": "count"
} Execution steps Include or exclude current value Computed output 5 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
};Continue learning Practice related DSA questions in the same topic.