Find an index where the sum of elements on the left equals the sum of elements on the right.
Given an integer array nums, find the middle index of the array.
A middle index is an index i such that the sum of all elements strictly to the left of i is equal to the sum of all elements strictly to the right of i.
If there are multiple valid middle indices, return the leftmost one. If no such index exists, return -1.
You are looking for a position that balances the array into two parts with equal sum, ignoring the element at the index itself.
nums of length n.nums[i] may be positive, negative, or zero.i such that:
sum(nums[0..i-1]) == sum(nums[i+1..n-1])-1.1 <= nExample 1
Input
nums = [2, 3, -1, 8, 4]
Output
3
Explanation
Left sum at index 3 is 2 + 3 + (-1) = 4, and right sum is 4.
Example 2
Input
nums = [1, -1, 4]
Output
2
Explanation
Left sum of index 2 is 1 + (-1) = 0, and right sum is 0.
Example 3
Input
nums = [2, 5]
Output
-1
Explanation
No index has equal sums on both sides.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.