Back to questionsDSA Visualizers: 388 Coding Questions Maximum Sum Combinations visualizer Learn Maximum Sum Combinations with a step-by-step Heaps visualizer, JavaScript source code and a worked example. Practice priority queues and top k.
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,
4,
2,
3
],
"b": [
2,
5,
1,
6
],
"k": 4
} Code Visuals
JavaScript reference implementation · highlighted trace statement
1 const solve = ({
2 a,b,k
3 },s)=>{
4 a.sort((x,y)=>y-x);
5 b.sort((x,y)=>y-x);
6 let q=[[a[0 ]+b[0 ],0 ,0 ]],seen=new Set(["0 ,0 "]),o=[];
7 while (o.length<k&&q.length){
8 q.sort((a,b)=>b[0 ]-a[0 ]);
9 let [v,i,j]=q.shift();
10 o.push(v);
11 for (let [x,y] of [[i+1 ,j],[i,j+1 ]])if (x<a.length&&y<b.length&&!seen.has(x+","+y)){
12 seen.add(x+","+y);
13 q.push([a[x]+b[y],x,y])
14 }
15 s("Extract largest candidate",{
16 A:a,B:b,Frontier:q,Output:o
17 })
18 }
19 return o
20 };
Execution canvas Active ChangedSTEP 1 Extract largest candidate Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.
Worked example and algorithm walkthrough Topic: Heaps · Priority queues and top K
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,
4,
2,
3
],
"b": [
2,
5,
1,
6
],
"k": 4
} Execution steps Extract largest candidate Computed output [
10,
9,
9,
8
] JavaScript reference implementation const solve = ({
a,b,k
},s)=>{
a.sort((x,y)=>y-x);
b.sort((x,y)=>y-x);
let q=[[a[0]+b[0],0,0]],seen=new Set(["0,0"]),o=[];
while(o.length<k&&q.length){
q.sort((a,b)=>b[0]-a[0]);
let [v,i,j]=q.shift();
o.push(v);
for(let [x,y] of [[i+1,j],[i,j+1]])if(x<a.length&&y<b.length&&!seen.has(x+","+y)){
seen.add(x+","+y);
q.push([a[x]+b[y],x,y])
}
s("Extract largest candidate",{
A:a,B:b,Frontier:q,Output:o
})
}
return o
};Continue learning Practice related DSA questions in the same topic.