java.lang.Object
g1901_2000.s1991_find_the_middle_index_in_array.Solution

public class Solution extends Object
1991 - Find the Middle Index in Array.<p>Easy</p> <p>Given a <strong>0-indexed</strong> integer array <code>nums</code>, find the <strong>leftmost</strong> <code>middleIndex</code> (i.e., the smallest amongst all the possible ones).</p> <p>A <code>middleIndex</code> is an index where <code>nums[0] + nums[1] + ... + nums[middleIndex-1] == nums[middleIndex+1] + nums[middleIndex+2] + ... + nums[nums.length-1]</code>.</p> <p>If <code>middleIndex == 0</code>, the left side sum is considered to be <code>0</code>. Similarly, if <code>middleIndex == nums.length - 1</code>, the right side sum is considered to be <code>0</code>.</p> <p>Return <em>the <strong>leftmost</strong></em> <code>middleIndex</code> <em>that satisfies the condition, or</em> <code>-1</code> <em>if there is no such index</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [2,3,-1,8,4]</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> The sum of the numbers before index 3 is: 2 + 3 + -1 = 4</p> <p>The sum of the numbers after index 3 is: 4 = 4</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,-1,4]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> The sum of the numbers before index 2 is: 1 + -1 = 0</p> <p>The sum of the numbers after index 2 is: 0</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = [2,5]</p> <p><strong>Output:</strong> -1</p> <p><strong>Explanation:</strong> There is no valid middleIndex.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 100</code></li> <li><code>-1000 <= nums[i] <= 1000</code></li> </ul> <p><strong>Note:</strong> This question is the same as 724</p>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • findMiddleIndex

      public int findMiddleIndex(int[] nums)