|
| 1 | +<h2><a href="https://leetcode.com/problems/find-pivot-index">724. Find Pivot Index</a></h2><h3>Easy</h3><hr><p>Given an array of integers <code>nums</code>, calculate the <strong>pivot index</strong> of this array.</p> |
| 2 | + |
| 3 | +<p>The <strong>pivot index</strong> is the index where the sum of all the numbers <strong>strictly</strong> to the left of the index is equal to the sum of all the numbers <strong>strictly</strong> to the index's right.</p> |
| 4 | + |
| 5 | +<p>If the index is on the left edge of the array, then the left sum is <code>0</code> because there are no elements to the left. This also applies to the right edge of the array.</p> |
| 6 | + |
| 7 | +<p>Return <em>the <strong>leftmost pivot index</strong></em>. If no such index exists, return <code>-1</code>.</p> |
| 8 | + |
| 9 | +<p> </p> |
| 10 | +<p><strong class="example">Example 1:</strong></p> |
| 11 | + |
| 12 | +<pre> |
| 13 | +<strong>Input:</strong> nums = [1,7,3,6,5,6] |
| 14 | +<strong>Output:</strong> 3 |
| 15 | +<strong>Explanation:</strong> |
| 16 | +The pivot index is 3. |
| 17 | +Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11 |
| 18 | +Right sum = nums[4] + nums[5] = 5 + 6 = 11 |
| 19 | +</pre> |
| 20 | + |
| 21 | +<p><strong class="example">Example 2:</strong></p> |
| 22 | + |
| 23 | +<pre> |
| 24 | +<strong>Input:</strong> nums = [1,2,3] |
| 25 | +<strong>Output:</strong> -1 |
| 26 | +<strong>Explanation:</strong> |
| 27 | +There is no index that satisfies the conditions in the problem statement.</pre> |
| 28 | + |
| 29 | +<p><strong class="example">Example 3:</strong></p> |
| 30 | + |
| 31 | +<pre> |
| 32 | +<strong>Input:</strong> nums = [2,1,-1] |
| 33 | +<strong>Output:</strong> 0 |
| 34 | +<strong>Explanation:</strong> |
| 35 | +The pivot index is 0. |
| 36 | +Left sum = 0 (no elements to the left of index 0) |
| 37 | +Right sum = nums[1] + nums[2] = 1 + -1 = 0 |
| 38 | +</pre> |
| 39 | + |
| 40 | +<p> </p> |
| 41 | +<p><strong>Constraints:</strong></p> |
| 42 | + |
| 43 | +<ul> |
| 44 | + <li><code>1 <= nums.length <= 10<sup>4</sup></code></li> |
| 45 | + <li><code>-1000 <= nums[i] <= 1000</code></li> |
| 46 | +</ul> |
| 47 | + |
| 48 | +<p> </p> |
| 49 | +<p><strong>Note:</strong> This question is the same as 1991: <a href="https://leetcode.com/problems/find-the-middle-index-in-array/" target="_blank">https://leetcode.com/problems/find-the-middle-index-in-array/</a></p> |
0 commit comments