# Find Pivot Index

Given an array of integers`nums`, write a method that returns the "pivot" index of this array.

We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.

If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.

**Example 1:**

```
Input: 
nums = [1, 7, 3, 6, 5, 6]
Output: 3
Explanation: 
The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.
Also, 3 is the first index where this occurs.
```

**Example 2:**

```
Input: 
nums = [1, 2, 3]
Output: -1
Explanation: 
There is no index that satisfies the conditions in the problem statement.
```

**Note:**

The length of `nums` will be in the range `[0, 10000]`.

Each element `nums[i]` will be an integer in the range `[-1000, 1000]`.

## Analysis

**Brute-Force**

循环每一个元素，再依次计算左侧sum和右侧sum，比较大小

**Prefix-Sum**

数列求和问题中prefix sum是一个屡试不爽的方法，这一题也一样。直觉思路就是如何快速地求出元素的左右两侧的和。用prefix sum，则可以在O(1)时间得到每一个index左右两侧分别的和 (sum).

对于index `i`, 有：

`rightsum = Sum - nums[i] - leftsum`

其中`Sum`是数列全体的和.

两种实现方式：

* 用额外O(n)空间存prefix sum
* 只存数列总sum，和循环过程中的leftsum

注意这题对于empty sum的定义，有一个test case：

```
Input
[-1,-1,-1,0,1,1]

Expected output is: 0.
```

## Solution

Prefix Sum (using array to store prefix sum) - O(n) time, O(n) space

```java
class Solution {
    public int pivotIndex(int[] nums) {
        if (nums == null || nums.length == 0) {
            return -1;
        }
        int[] prefixSum = new int[nums.length + 1];
        prefixSum[0] = 0;
        for (int i = 0; i < nums.length; i++) {
            prefixSum[i + 1] = prefixSum[i] + nums[i];
        }
        for (int i = 1; i < prefixSum.length; i++) {
            if (prefixSum[i - 1] == prefixSum[prefixSum.length - 1] - prefixSum[i]) {
                return i - 1;
            }
        }
        return -1;
    }
}
```

Prefix Sum - O(n) time, O(1) space

```java
class Solution {
    public int pivotIndex(int[] nums) {
        int sum = 0, leftsum = 0;
        for (int x: nums) sum += x;
        for (int i = 0; i < nums.length; ++i) {
            if (leftsum == sum - leftsum - nums[i]) return i;
            leftsum += nums[i];
        }
        return -1;
    }
}
```

Brute-Force - Time: O(n^2), Space O(1)

```java
class Solution {
    public int pivotIndex(int[] nums) {
        if (nums == null || nums.length < 3) {
            return -1;
        }
        for (int i = 0; i < nums.length; i++) {
            int left = i - 1, right = i + 1;
            int sumLeft = 0, sumRight = 0;

            while (left >= 0) {
                sumLeft += nums[left--];
            }
            while (right < nums.length) {
                sumRight += nums[right++];
            }
            if (sumLeft == sumRight) {
                return i;
            }
        }
        return -1;
    }
}
```

## Reference

<https://leetcode.com/articles/find-pivot-index/>

<https://leetcode.com/problems/find-pivot-index/discuss/109249/Java-6-liner>
