Prefix Sum· 15 min

Find Pivot Index

LeetCode #724 · Easy · Interactive step-by-step visualization

Setup

Example inputnums = [1,7,3,6,5,6]
Step 1 / 8

Current step

1 / 8

Test index 0. The pivot itself belongs to neither side, so subtract it from the total. Left 0; right 27.

Step log

nums

Cursor
1
0
7
1
3
2
6
3
5
4
6
5

total

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