Find Pivot Index

Given an integer array nums, return the pivot index: the leftmost index where the sum of all numbers strictly to its left equals the sum of all numbers strictly to its right. An empty side sums to 0. If no such index exists, return -1.

30:00

Find Pivot Index
easy
Free written solution
Topics
Companies

Given an integer array nums, return the pivot index: the leftmost index where the sum of all numbers strictly to its left equals the sum of all numbers strictly to its right. An empty side sums to 0. If no such index exists, return -1.

Example 1:
Input: {"nums":[1,7,3,6,5,6]}
Output: 3
Constraints:
  • 1≤nums.length≤1041 \leq \text{nums.length} \leq 10^4

  • −1000≤nums[i]≤1000-1000 \leq \text{nums}[i] \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,7,3,6,5,6]}

Look for an index where the weight on its left balances the weight on its right.

0left?right
total = ?

nums: left pan | pivot | right pan

1

0

7

1

3

2

6

3

5

4

6

5

Variables
VariableValue
n6
DepthFunction Call
Stack empty
0/11