Find Pivot Index

Prefix Sums, problem 2 of 7

Find Pivot Index

Easy

LC #724

prefix sumarray

Not attempted yet

Given an integer array nums, return the pivot index: the leftmost index i where the sum of the numbers strictly to its left equals the sum of the numbers strictly to its right. nums[i] itself is in neither side. An empty side sums to 0.

Return -1 if no such index exists.

Example 1

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

Left of index 3: 1 + 7 + 3 = 11. Right: 5 + 6 = 11.

Example 2

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

Example 3

Input: nums = [2, 1, -1]
Output: 0

Left of index 0 is empty (0); right is 1 + (-1) = 0.

Constraints

  • 1 ≤ len(nums) ≤ 2 · 10⁵
  • -1000 ≤ nums[i] ≤ 1000

Python

Loading draft…

Test results

8 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 5 hidden

Run examples, then submit all tests.