Back to questionsDSA Visualizers: 388 Coding Questions Largest Divisible Subset visualizer Learn Largest Divisible Subset with a step-by-step Dynamic Programming visualizer, JavaScript source code and a worked example. Practice stocks and increasing subsequences.
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 / 5 Execution step
Prediction modePanel width Reference example · JavaScript
Inspect reference input {
"a": [
1,
2,
4,
8
],
"mode": "div"
} Code Visuals
JavaScript reference implementation · highlighted trace statement
1 const solve = ({
2 a,mode
3 },s)=>{
4 if (mode==="div")a.sort((x,y)=>x-y);
5 let d=a.map(()=>1 ),p=a.map((_,i)=>i),c=a.map(()=>1 );
6 for (let i=0 ;
7 i<a.length;
8 i++){
9 for (let j=0 ;
10 j<i;
11 j++)if (mode==="div"?a[i]%a[j]===0 :a[j]<a[i]){
12 if (d[j]+1 >d[i]){
13 d[i]=d[j]+1 ;
14 p[i]=j;
15 c[i]=c[j]
16 }else if (d[j]+1 ===d[i])c[i]+=c[j]
17 }
18 s("Extend valid predecessor chains",{
19 Array:a,DP:d,Parents:p,Counts:c
20 },[i])
21 }
22 let max=Math.max(...d);
23 if (mode==="count")return c.reduce((n,x,i)=>n+(d[i]===max?x:0 ),0 );
24 let i=d.indexOf(max),o=[a[i]];
25 while (p[i]!==i){
26 i=p[i];
27 o.unshift(a[i])
28 }
29 return o
30 };
Execution canvas Active ChangedSTEP 1 Extend valid predecessor chains 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 · Stocks and increasing subsequences
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,
4,
8
],
"mode": "div"
} Execution steps Extend valid predecessor chains Computed output [
1,
2,
4,
8
] JavaScript reference implementation const solve = ({
a,mode
},s)=>{
if(mode==="div")a.sort((x,y)=>x-y);
let d=a.map(()=>1),p=a.map((_,i)=>i),c=a.map(()=>1);
for(let i=0;
i<a.length;
i++){
for(let j=0;
j<i;
j++)if(mode==="div"?a[i]%a[j]===0:a[j]<a[i]){
if(d[j]+1>d[i]){
d[i]=d[j]+1;
p[i]=j;
c[i]=c[j]
}else if(d[j]+1===d[i])c[i]+=c[j]
}
s("Extend valid predecessor chains",{
Array:a,DP:d,Parents:p,Counts:c
},[i])
}
let max=Math.max(...d);
if(mode==="count")return c.reduce((n,x,i)=>n+(d[i]===max?x:0),0);
let i=d.indexOf(max),o=[a[i]];
while(p[i]!==i){
i=p[i];
o.unshift(a[i])
}
return o
};Continue learning Practice related DSA questions in the same topic.