← DSA Atlas
Dedicated problem page · #724

Find Pivot Index

EasyPrefix Sum and Difference ArrayRunning prefix versus suffix balancePrefix sum with total-based right sum
Solve on LeetCode ↗
724
EasyPrefix Sum and Difference ArrayPrefix sum with total-based right sumRunning prefix versus suffix balance

Find Pivot Index

Given an integer array nums, return the leftmost pivot index where the sum of all elements strictly to its left equals the sum of all elements strictly to its right. If no such index exists, return -1. The pivot's own value is excluded from both sides.

Open official problem prompt ↗
In plain English

Find the first split point where the array balances, left weight equal to right weight.

Picture it like this

Balancing a seesaw: you slide the fulcrum along a plank of weights until the load on the left matches the load on the right, ignoring the plank cell directly under the fulcrum.

Example
Input
nums = [1, 7, 3, 6, 5, 6]
Output
3
Why
Left of index 3 is 1+7+3=11 and right is 5+6=11, and it is the smallest such index
Constraints
1 <= nums.length <= 10^4-1000 <= nums[i] <= 1000
Pattern lesson

See the pattern, then code

Running prefix versus suffix balance
Recognition clue

Balancing the sum on either side of a moving split point is a prefix-sum problem: you need the left sum and can derive the right sum from the total.

Prefix Sum and Difference Array

Repeated range queries, subarray totals, balanced counts, or batched range updates.. As you scan, the left sum grows one element at a time. The right sum is simply total - left - nums[i], so no second array is needed; check equality at each index.

New words, made simpleKnow these before the algorithm
Pivot index
The position whose left-side sum equals its right-side sum, excluding itself.
Total sum
The sum of the whole array, used to derive the right side as total - left - current.
Approaches from first idea to best ideaCompare the trade-offs
ApproachHow it thinksCost
Recompute both sides

Redundant re-summing; unnecessary when a running total suffices.

For each candidate index, sum everything left and everything right separately.

Time O(n^2)Space O(1)
The rule we keep true

Invariant

Before testing index i, left equals the sum of nums[0..i-1], so total - left - nums[i] is exactly the sum of nums[i+1..end].

Why this is correct

Reasoning

The whole array splits into left part, the pivot element, and right part. Since total = left + nums[i] + right, the right part must equal total - left - nums[i]; comparing that to left directly tests the balance condition.

The algorithm in three movesSay these aloud before coding
1Compute the total sum of the array

total=28

2Walk the array keeping a running left sum, starting at 0

i3: left=1+7+3=11

3At each index test whether left equals total - left - nums[i]

right=28-11-6=11

4If it matches return the index; otherwise add nums[i] to left and continue

11==11 => return 3

Visual trace

Follow the data, one decision at a time

Input snapshotHighlighted cells are involved in this example
10
71
32
63
54
65
1 · Readnums = [1,7,3,6,5,6]
2 · AskWhat is the total?
3 · Update statetotal=28, left=0
4 · ResultReady to scan
Key takeaway

At index 3 the left sum (11) equals the right sum (11), so 3 is the pivot.

Code walkthrough

Read the solution in small chunks

Python 3

Do not memorize the whole program. Connect each group of lines to one job in the algorithm.

  1. 1
    Lines 3-4Total and running left

    Compute the full sum once; left starts empty because nothing is to the left of index 0.

  2. 2
    Lines 5-9Balance test then advance

    Check equality before adding the current value so the current element stays excluded from left.

Make it stick

Edge cases, mistakes, and one self-check

E

Edge cases to test

  • Index 0 can be the pivot when the rest of the array sums to 0
  • The last index can be the pivot when everything before it sums to 0
  • A single-element array returns 0 since both empty sides sum to 0
  • Negative values are handled since sums may be negative
!

Common beginner mistakes

  • Adding nums[i] to left before the comparison, which wrongly includes the pivot on the left
  • Building a separate right-sum array when total - left - nums[i] is enough
  • Returning the last valid index instead of the first, violating the leftmost requirement
Check your understanding

Why is the equality checked before updating left with the current element?