Back to DSA sheet

Find Pivot Index

EasyArrays

The pivot index is the index where the sum of all numbers strictly to its left equals the sum of all numbers strictly to its right. Return the leftmost pivot index, or -1 if none exists.

Input format (stdin): the first line has n. The second line has n integers. Output the pivot index or -1.

Examples
Input: 6 1 7 3 6 5 6
Output: 3
Left of index 3 sums to 11 (1+7+3) and right also sums to 11 (5+6).
Input: 3 1 2 3
Output: -1
No index balances the two sides.
Constraints
  • 1 <= n <= 10^4
  • -1000 <= nums[i] <= 1000
Sheets
NeetCode 250
find-pivot-index.cpp3 sample tests
Loading editor
Test results

Run the sample tests to check your solution against expected output.

Custom input (stdin)
Output

Run your code to see its output.