Back to ArraysFind Pivot Index
Easy

Find Pivot Index

EasyRecommendedPrefix Sum

Given an array, find the leftmost index where the sum of all elements to its left equals the sum of all elements to its right. Return -1 if no such index exists.

Examples

Example 1

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

Explanation: At index 3, the left sum (1+7+3=11) equals the right sum (5+6=11), and it's the leftmost such index.

Example 2

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

Explanation: At index 0, the left side is empty (sum 0), and the right side (1 + -1) also sums to 0 — a valid pivot right at the boundary.

Example 3

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

Explanation: No index splits the array into two equal-sum halves, so -1 signals that no pivot exists.

Constraints

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

Loading editor…

Code execution is coming soon.