Find Pivot Index
Given an array of integers nums, find its pivot index: an index where the sum of all numbers strictly to its left equals the sum of all numbers strictly to its right. The value at the pivot itself belongs to neither side.
At index 0 the left side is empty (sum 0); at the last index the right side is empty. If several indices qualify, return the leftmost one. If none does, return -1.
Constraints
1 <= nums.length <= 10^4-1000 <= nums[i] <= 1000
Examples
Example 1
Input: nums = [2,3,-1,8,4] Output: 3
Left of index 3: 2 + 3 + -1 = 4. Right of it: 4. No earlier index balances.
Example 2
Input: nums = [1,2,3] Output: -1
No index has equal sums on both sides, so return -1.
Example 3
Input: nums = [5,3,-3] Output: 0
Nothing is left of index 0 (sum 0), and 3 + -3 = 0 on the right.
JavaScript you'll use: Arrays: create, read, update Iterating and transforming arrays
/**
* @param {number[]} nums
* @return {number}
*/
function pivotIndex(nums) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.