Workspace Maximum Consecutive Ones
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Maximum Consecutive Ones visualizer

Learn Maximum Consecutive Ones with a step-by-step Arrays visualizer, JavaScript source code and a worked example. Practice fundamentals.

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 / 6Execution step
Reference example · JavaScript
Inspect reference input
[
  1,
  0,
  1,
  1,
  1
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
let r=0,b=0;
a.forEach((x,i)=>{
r=x===1?r+1:0;
b=Math.max(r,b);
s("Update consecutive run",{
Array:a,Run:[r],Best:[b]
},[i])
});
return b
};

Execution canvas

Active Changed
STEP 1

Update consecutive run

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Array5 items
Run1 items
Best1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Arrays · Fundamentals

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

[
  1,
  0,
  1,
  1,
  1
]

Execution steps

  1. Update consecutive run

Computed output

3
JavaScript reference implementation
const solve = (a,s)=>{
  let r=0,b=0;
  a.forEach((x,i)=>{
    r=x===1?r+1:0;
    b=Math.max(r,b);
    s("Update consecutive run",{
      Array:a,Run:[r],Best:[b]
    },[i])
  });
  return b
};

Continue learning

Practice related DSA questions in the same topic.