Find Pivot Index
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.
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].
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.
An integer array nums of length n.
One integer: the smallest pivot index, or -1 if there is none.
Left of index 3: 1 + 7 + 3 = 11. Right of index 3: 5 + 6 = 11. No earlier index works.
No index has equal sums on both sides.
Index 0 has nothing on its left (sum 0). Its right side is 1 + (−1) = 0.
- • 1 ≤ n ≤ 10^4
- • −1000 ≤ nums[i] ≤ 1000
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
For one fixed index i, which numbers are on the left and which are on the right? Where does nums[i] go?
- 2.
If I know the left sum for index i, how do I get the left sum for index i + 1?
- 3.
Left part + nums[i] + right part — what does that add up to, for every i?
- 4.
Can negative numbers break any idea that relies on sums only growing?
- 5.
If several indices are pivots, which one do I return, and in what order should I check them?
Brute force approach
Better approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | For each index, loop to sum the left side and loop again to sum the right side | O(N²) | O(1) |
| Better | One pass builds all left sums, one pass builds all right sums, then compare | O(N) | O(N) |
| Optimal | Compute the total once; right side = total − left − nums[i] | O(N) | O(1) |
Why does this belong to this pattern?
- “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.
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.
Don't make this mistake
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.
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.
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.
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.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Prefix Sum translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.