Workspace Find Pivot Index
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Find Pivot Index visualizer

Learn Find Pivot Index with a step-by-step Arrays visualizer, JavaScript source code and a worked example. Practice hashing and prefix sums.

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

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
let l=0,r=a.reduce((x,y)=>x+y,0);
for(let i=0;
i<a.length;
i++){
r-=a[i];
s("Compare side sums",{
Array:a,Left:[l],Right:[r]
},[i]);
if(l===r)return i;
l+=a[i]
}
return -1
};

Execution canvas

Active Changed
STEP 1

Compare side sums

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

Array6 items
Left1 items
Right1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Arrays · Hashing and prefix sums

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,
  7,
  3,
  6,
  5,
  6
]

Execution steps

  1. Compare side sums

Computed output

3
JavaScript reference implementation
const solve = (a,s)=>{
  let l=0,r=a.reduce((x,y)=>x+y,0);
  for(let i=0;
  i<a.length;
  i++){
    r-=a[i];
    s("Compare side sums",{
      Array:a,Left:[l],Right:[r]
    },[i]);
    if(l===r)return i;
    l+=a[i]
  }
  return -1
};

Continue learning

Practice related DSA questions in the same topic.