211. Find Pivot Index

Easy
Prefix Sum Balance
Array

Problem

Given an integer array nums, return the leftmost pivot index where the sum of elements strictly to the left equals the sum of elements strictly to the right. Return -1 if no pivot exists.

Examples

Example 1

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

Example 2

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

Example 3

Input: [2,1,-1]
Output: 0
Constraints

1 <= nums.length <= 10000

-1000 <= nums[i] <= 1000

Hints

?? Compute the total sum once.

?? As you scan, derive the right sum from total - left - current.

Expected Complexity
Time: O(n)
Space: O(1)
Follow-up

Can you solve it without storing a prefix-sum array?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
[1,7,3,6,5,6]
Expected
3