2Brute2Best
🔥0

Find Pivot Index

Easy·LeetCode #724Basic Anchor
LeetCode
30-Second Mental ModelPrefix Sum (prefix + suffix)
Complexity:O(N²)→O(N)
Key Insight

Add up the whole array once to get total. Walk from the left keeping left, the sum of everything before i. The right side is whatever is not on the left and is not nums[i], so right = total − left − nums[i]. Return the first i where left == right.

Core Validity Invariant

Index i is a pivot when left == total − left − nums[i], where left covers only the numbers before i and neither side includes nums[i].

01

Problem

You are given an integer array nums.

An index i is a pivot if the sum of all numbers strictly to its left equals the sum of all numbers strictly to its right. nums[i] itself belongs to neither side. If there is nothing on one side, that side's sum is 0.

Return the leftmost pivot index. If no index is a pivot, return -1.

Input

An integer array nums of length n.

Output

One integer: the smallest pivot index, or -1 if there is none.

Example 1
Input: nums = [1, 7, 3, 6, 5, 6]
Output: 3

Left of index 3: 1 + 7 + 3 = 11. Right of index 3: 5 + 6 = 11. No earlier index works.

Example 2
Input: nums = [1, 2, 3]
Output: -1

No index has equal sums on both sides.

Example 3
Input: nums = [2, 1, -1]
Output: 0

Index 0 has nothing on its left (sum 0). Its right side is 1 + (−1) = 0.

Constraints
  • • 1 ≤ n ≤ 10^4
  • • −1000 ≤ nums[i] ≤ 1000
02

First, think yourself

Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.

  1. 1.

    For one fixed index i, which numbers are on the left and which are on the right? Where does nums[i] go?

  2. 2.

    If I know the left sum for index i, how do I get the left sum for index i + 1?

  3. 3.

    Left part + nums[i] + right part — what does that add up to, for every i?

  4. 4.

    Can negative numbers break any idea that relies on sums only growing?

  5. 5.

    If several indices are pivots, which one do I return, and in what order should I check them?

03

Brute force approach

04

Better approach

05

Optimized approach

06

Why does this algorithm work?

07

Dry run

08

All approaches compared

ApproachIdeaTimeSpace
Brute ForceFor each index, loop to sum the left side and loop again to sum the right sideO(N²)O(1)
BetterOne pass builds all left sums, one pass builds all right sums, then compareO(N)O(N)
OptimalCompute the total once; right side = total − left − nums[i]O(N)O(1)
09

Why does this belong to this pattern?

Clues in the problem statement
  • “sum of all numbers strictly to the left”A prefix sum: a running total from the left end, stopping just before i.
  • “sum of all numbers strictly to the right”A suffix sum: a running total from the right end — or simply the total minus everything else.
  • “each index”Every index needs information from both sides → a left pass and a right pass, then combine per index.
  • “leftmost”Scan from the left and return on the first match.
Per-index answer+needs the sum on both sides+left + self + right = total→Prefix Sum (prefix + suffix)

This is the anchor of the prefix + suffix sub-pattern: every index needs something from its left and something from its right. Here that something is a sum, and sums have a shortcut: right = total − left − nums[i], so one running variable replaces the whole suffix array. Later questions in this group use the same left-pass / right-pass idea with products and counts, where the shortcut works differently.

10

Don't make this mistake

Counting nums[i] on one of the sides
✗ left += nums[i]; int right = total - left; if (left == right) return i;
✓ int right = total - left - nums[i]; if (left == right) return i; left += nums[i];

The pivot itself belongs to neither side. If you add nums[i] to left before the check (or forget to subtract it from the right), you are testing a different question. Check first, then add.

Returning the last pivot instead of the first
✗ int ans = -1; for (...) if (left == right) ans = i; // keeps overwriting return ans;
✓ for (...) if (left == right) return i;

With zeros there can be several pivots, e.g. [3, −2, 4, 1, 0, 0, 2, −1, 5, 0] has pivots at 4 and 5. The question wants the leftmost one, so return immediately.

Pattern confusion: two pointers from both ends
✗ while (l < r) { if (leftSum < rightSum) leftSum += nums[l++]; else rightSum += nums[r--]; }
✓ Compute total once, then walk left to right with right = total − left − nums[i].

Moving the pointer on the smaller side only works if adding a number always makes a side bigger. With negative numbers, adding can make a side smaller, so there is no safe direction to move and the pointers can skip the real pivot. Prefix sums don't care about the sign — they just compare exact totals.

Forgetting that an empty side counts as 0

Index 0 has nothing on its left, and the last index has nothing on its right. Those sides are 0, not "invalid". [2, 1, −1] has its pivot at index 0. Starting left at 0 handles this with no special case.

11

Want to implement this?

Write it yourself on LeetCode, then come back and compare your code with the solutions above.

Open coding workspace
How did it go?
Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Prefix Sum translate into production architectures and low-level memory mechanics.