Given a 0-indexed integer array nums, find the leftmost middleIndex (i.e., the smallest amongst all the possible ones).

A middleIndex is an index where nums[0] + nums[1] + ... + nums[middleIndex-1] == nums[middleIndex+1] + nums[middleIndex+2] + ... + nums[nums.length-1].

If middleIndex == 0, the left side sum is considered to be 0. Similarly, if middleIndex == nums.length - 1, the right side sum is considered to be 0.

Return the leftmost middleIndex that satisfies the condition, or -1 if there is no such index.

&nbsp;
Example 1:

Input: nums = [2,3,-1,8,4]
Output: 3
Explanation: The sum of the numbers before index 3 is: 2 + 3 + -1 = 4
The sum of the numbers after index 3 is: 4 = 4


Example 2:

Input: nums = [1,-1,4]
Output: 2
Explanation: The sum of the numbers before index 2 is: 1 + -1 = 0
The sum of the numbers after index 2 is: 0


Example 3:

Input: nums = [2,5]
Output: -1
Explanation: There is no valid middleIndex.


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 100
	-1000 &lt;= nums[i] &lt;= 1000


&nbsp;
Note: This question is the same as&nbsp;724:&nbsp;https://leetcode.com/problems/find-pivot-index/

class Solution:
    def findMiddleIndex(self, nums: List[int]) -> int:
        A = [0] + list(accumulate(nums)) + [0]
        total, n = sum(nums), len(nums)
        for i in range(n):
            if A[i] == total - A[i] - nums[i]:
                return i
        return -1   
class Solution {
    public int findMiddleIndex(int[] nums) {

        for(int i=0;i<nums.length;i++)
        {
            int rsum=0;
            int lsum=0;

            for(int k=0;k<i;k++) lsum+=nums[k];
            for(int k=i+1;k<nums.length;k++) rsum+=nums[k];

            if(lsum==rsum){
                return i;
            }
        }

        return -1;

    }
}
class Solution {
public:
  
    int findMiddleIndex(vector<int>& nums) 
    {
        int n=nums.size();
        int left_sum=0;
        int right_sum=0;
        for(int i=0;i<n;i++)
        {
            right_sum+=nums[i];
        }
        for(int i=0;i<n;i++)
        {
            right_sum-=nums[i];
            if(left_sum==right_sum)
            {
                return i;
            }
            left_sum+=nums[i];
        }
        return -1;
    }
};
var findMiddleIndex = function(nums) {
    for (let i = 0; i < nums.length; i++) {
        let leftNums = nums.slice(0, i).reduce((a, b) => a + b, 0);
        let rightNums = nums.slice(i + 1).reduce((a, b) => a + b, 0);
        if (leftNums === rightNums) {
            return i;
        }
    }
    return -1;
};
Find the Middle Index in Array