Find Pivot Index - Problem

Given an array of integers nums, calculate the pivot index of this array.

The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the right of the index.

If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the array.

Return the leftmost pivot index. If no such index exists, return -1.

Input & Output

Example 1 — Basic Pivot Found
$ Input: nums = [1,7,3,6,5,6]
Output: 3
💡 Note: At index 3: left sum = 1+7+3 = 11, right sum = 5+6 = 11. Both sums are equal, so pivot index is 3.
Example 2 — No Pivot Exists
$ Input: nums = [1,2,3]
Output: -1
💡 Note: No index where left sum equals right sum. Index 0: left=0, right=5. Index 1: left=1, right=3. Index 2: left=3, right=0.
Example 3 — Pivot at Edge
$ Input: nums = [2,1,-1]
Output: 0
💡 Note: At index 0: left sum = 0 (no elements), right sum = 1+(-1) = 0. Both are equal, so pivot is at index 0.

Constraints

  • 1 ≤ nums.length ≤ 104
  • -1000 ≤ nums[i] ≤ 1000

Visualization

Tap to expand
Find Pivot Index: Balance Point in Array173656012345Left Sum: 1+7+3 = 11Right Sum: 5+6 = 11Pivot Index = 3 (Left Sum = Right Sum = 11)
Understanding the Visualization
1
Input Array
Given array [1,7,3,6,5,6] with indices 0-5
2
Find Balance
Look for index where left sum equals right sum
3
Output
Return index 3 where both sides sum to 11
Key Takeaway
🎯 Key Insight: Use total sum to calculate right sum efficiently without nested loops
Asked in
Facebook 35 Amazon 28 Microsoft 22
85.0K Views
Medium Frequency
~15 min Avg. Time
2.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen