Find Pivot Index
EasyArray
Description
Given an array of integers nums, calculate the pivot index where the sum of numbers to the left equals the sum of numbers to the right. Return -1 if no such index exists.
Examples
Input:
nums = [1,7,3,6,5,6]Output:
3Explanation:
Sum left of index 3 is 1+7+3=11, right is 5+6=11.
Input:
nums = [2,1,-1]Output:
0Explanation:
At index 0, left sum is 0 and right sum is 1+(-1)=0. Both sides are equal.
Input:
nums = [4,-2,3,1,-1,2]Output:
2Explanation:
Sum left of index 2 is 4+(-2)=2, right is 1+(-1)+2=2. This example demonstrates handling negative numbers in the array.
Constraints
- •
1 ≤ nums.length ≤ 10⁴
Ready to solve this problem?
Practice solo and sharpen your skills for technical interviews.