Prefix Sum· 15 min
Find Pivot Index
LeetCode #724 · Easy · Interactive step-by-step visualization
Setup
Example input
nums = [1,7,3,6,5,6]Step 1 / 8
Current step
1 / 8Test index 0. The pivot itself belongs to neither side, so subtract it from the total. Left 0; right 27.
Step log
nums
Cursor
1
07
13
26
35
46
5total
28
left
0
right
27
Active cursor
Comparing
Best or completed
Leaving or removed
find-pivot-index.java
1public int pivotIndex(int[] nums) {2 int total = Arrays.stream(nums).sum();3 int left = 0;4 for (int i = 0; i < nums.length; i++) {5 int right = total - left - nums[i];6 if (left == right) return i;7 left += nums[i];8 }9 return -1;10}● Java
Time
O(n)
Space
O(1)
Pattern
array scan
Frames
8
