Find Pivot Index

Easy
186.

Find Pivot Index

Find Pivot Index is an easy coding problem on arrays and prefix sum, asked in Amazon, Google, Adobe and Meta interviews.

Given an array of integers nums, return the leftmost pivot index: the index where the sum of all numbers strictly to its left equals the sum of all numbers strictly to its right.

At index 0 the left sum is 0, and at the last index the right sum is 0. If no pivot index exists, return -1.

Example 1

Input
nums = [1,7,3,6,5,6]
Output
3
Explanation
1+7+3 = 11 on the left and 5+6 = 11 on the right.

Example 2

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

Example 3

Input
nums = [2,1,-1]
Output
0
Explanation
The left sum is 0 and the right sum is 1 + (-1) = 0.

Constraints

  • 1 <= nums.length <= 40
  • -1000 <= nums[i] <= 1000
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number[]} nums
 * @return {number}
 */
var pivotIndex = function(nums) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Find Pivot Index — Solution in Python, Java & C++

Find Pivot Index solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on arrays and prefix sum.

Find Pivot Index is an easy coding problem on arrays and prefix sum, asked in Amazon, Google, Adobe and Meta interviews.

Problem statement

Given an array of integers nums, return the leftmost pivot index: the index where the sum of all numbers strictly to its left equals the sum of all numbers strictly to its right.

At index 0 the left sum is 0, and at the last index the right sum is 0. If no pivot index exists, return -1.

Example 1

Input: nums = [1,7,3,6,5,6]
Output: 3
Explanation: 1+7+3 = 11 on the left and 5+6 = 11 on the right.

Example 2

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

Example 3

Input: nums = [2,1,-1]
Output: 0
Explanation: The left sum is 0 and the right sum is 1 + (-1) = 0.

Constraints

  • 1 <= nums.length <= 40
  • -1000 <= nums[i] <= 1000

Hints for Find Pivot Index

  1. Hint 1

    Recomputing both sides at every index is O(n²) — the total never changes, so compute it once.

  2. Hint 2

    Keep a running left sum; the right sum is then total - left - nums[i], available in O(1).

  3. Hint 3

    Return at the first match, since the problem asks for the leftmost such index.

How to solve Find Pivot Index

The total never changes, so with a running left sum the right sum is total - left - nums[i] and each index is tested in O(1).

Approach

  1. Compute total.
  2. Walk i with left = 0; if left == total - left - nums[i], return i.
  3. Add nums[i] to left; return -1 after the loop.

Why it works

At index i the left sum excludes nums[i] and the right sum is everything else, which is exactly the identity used. Returning at the first match gives the leftmost pivot.

Complexity

  • Time — O(n)
  • Space — O(1)

Pitfalls

  • Add nums[i] to left after the comparison.
  • Index 0 with a zero-sum remainder is a valid pivot.

Reference solution

Python

def pivotIndex(nums):
    total = sum(nums)
    left = 0
    for i, x in enumerate(nums):
        if left == total - left - x:
            return i
        left += x
    return -1

Java

public static int pivotIndex(int[] nums) {
    int total = 0;
    for (int x : nums) total += x;
    int left = 0;
    for (int i = 0; i < nums.length; i++) {
        if (left == total - left - nums[i]) return i;
        left += nums[i];
    }
    return -1;
}

C++

int pivotIndex(vector<int>& nums) {
    int total = 0;
    for (int x : nums) total += x;
    int left = 0;
    for (int i = 0; i < (int) nums.size(); i++) {
        if (left == total - left - nums[i]) return i;
        left += nums[i];
    }
    return -1;
}

JavaScript

var pivotIndex = function(nums) {
    let total = 0;
    for (let i = 0; i < nums.length; i++) total += nums[i];
    let left = 0;
    for (let i = 0; i < nums.length; i++) {
        if (left === total - left - nums[i]) return i;
        left += nums[i];
    }
    return -1;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays · Prefix Sum