Find Pivot Index
Easy Prefix Sum
nums is a list of whole numbers, which may be negative or zero.
A pivot is a place i where the numbers before it add up to the same as the numbers after it; the number at i itself is in neither sum. Before the first place and after the last there is nothing, and nothing adds up to 0.
Return the first pivot, the one furthest to the left, or -1 if there is none.
Example 1
Before place 2 are 3 and 1, which add up to 4; after it are 2 and 2, which add up to 4 as well. Neither place 0 nor place 1 has equal sums.
Example 2
No place has the same sum on both sides: the sums before and after each place are 0 and 6, 2 and 9, -1 and 5, 3 and -1, 9 and 0, then 8 and 0.
Example 3
Constraints
1 ≤ nums.length ≤ 2 × 10⁵-1000 ≤ nums[i] ≤ 1000
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
See the AI roundWatch the explanation
More Prefix Sum
- Product of Array Except SelfAI round · Medium
- Subarray Sum Equals KAI round · Medium
- Contiguous ArrayAI round · Medium
- Running Sum of 1d ArrayVery easy
- Subarray Sums Divisible by KMedium