Community Problem

Find Pivot Index

Difficulty: Easy

Find the leftmost index where the sum of elements to its left equals the sum to its right.

Find Pivot Index

Find the leftmost index where the sum of elements to its left equals the sum to its right.

EASY
Free
arrays
prefix-sum
fundamentals

By CodeSnatch

March 8, 2026

·

Updated May 20, 2026

1,189 views

33

4.4 (9)

Prefix-sum review with a junior engineer last week, and this is the problem I keep reaching for as the cleanest one-pass intro. The official practice catalog has Range Sum Query and Subarray Sum Equals K, but it skipped the gateway exercise that makes prefix-sum click in under five minutes. So this entry plugs that gap.

Find Pivot Index

Given an integer array nums, return the leftmost pivot index. The pivot index is the index where the sum of all elements strictly to its left equals the sum of all elements strictly to its right. If the index is on the left edge of the array, the left sum is 0 because there are no elements to the left. The same is true for an index on the right edge. If no such index exists, return -1.

Examples

Example 1:

  • Input: nums = [1, 7, 3, 6, 5, 6]
  • Output: 3
  • Explanation: The pivot is index 3. Left sum = 1 + 7 + 3 = 11. Right sum = 5 + 6 = 11.

Example 2:

  • Input: nums = [1, 2, 3]
  • Output: -1
  • Explanation: There is no index where the left sum equals the right sum.

Example 3:

  • Input: nums = [2, 1, -1]
  • Output: 0
  • Explanation: The pivot is index 0. Left sum = 0 (no elements). Right sum = 1 + (-1) = 0.

Example 4:

  • Input: nums = [-1, -1, -1, -1, -1, 0]
  • Output: 2
  • Explanation: Left sum = -1 + -1 = -2. Right sum = -1 + -1 + 0 = -2.

Constraints

  • 1 <= nums.length <= 10^4
  • -1000 <= nums[i] <= 1000

Follow-up

Can you solve this in O(n) time and O(1) extra space, without precomputing a separate prefix-sum array?

Solution

Hints

0/4
Hint 1
Hint 2
Hint 3
Hint 4
All Problems