Find the Middle Index in Array (LeetCode 1991): Solution and Explanation
Return the leftmost index where the elements to its left add up to the same value as the elements to its right, or -1 if there is no such index.
Intuition
Think of the array as weights on a beam and the index as a pivot. The middle index is where the beam balances, with equal weight on each side of the pivot.
Summing both sides from scratch at every index costs O(n) per index. But the pieces are related: left + nums[i] + right = total.
So once the total is known, a running left sum is all you need. The right sum is total - left - nums[i], and moving the pivot one step right just adds nums[i] to left.
Approach: Prefix sum against the total
- Compute
total = sum(nums)and setleft = 0. - For each index
i, compareleftwithtotal - left - nums[i]. - If they are equal, return
i. Scanning from the left guarantees it is the leftmost answer. - Otherwise add
nums[i]toleftand continue. Return -1 after the loop.
Step-by-step example
nums = [2,3,-1,8,4]
- total = 16. At i = 0, left = 0 and right = 16 - 0 - 2 = 14. Not equal, so left becomes 2.
- At i = 1, left = 2 and right = 16 - 2 - 3 = 11. Not equal, so left becomes 5.
- At i = 2, left = 5 and right = 16 - 5 + 1 = 12. Not equal, so left becomes 4.
- At i = 3, left = 4 and right = 16 - 4 - 8 = 4. They match, so return 3.
Code: Python, Java, C++
1class Solution:
2 def findMiddleIndex(self, nums: List[int]) -> int:
3 total = sum(nums)
4 left = 0
5 for i, x in enumerate(nums):
6 if left == total - left - x:
7 return i
8 left += x
9 return -11class Solution {
2 public int findMiddleIndex(int[] nums) {
3 int total = 0;
4 for (int x : nums) total += x;
5 int left = 0;
6 for (int i = 0; i < nums.length; i++) {
7 if (left == total - left - nums[i]) {
8 return i;
9 }
10 left += nums[i];
11 }
12 return -1;
13 }
14}1class Solution {
2public:
3 int findMiddleIndex(vector<int>& nums) {
4 int total = accumulate(nums.begin(), nums.end(), 0);
5 int left = 0;
6 for (int i = 0; i < (int)nums.size(); i++) {
7 if (left == total - left - nums[i]) {
8 return i;
9 }
10 left += nums[i];
11 }
12 return -1;
13 }
14};Time and space complexity
O(n)
One pass to compute the total and one pass to scan for the pivot.
O(1)
Only the total and the running left sum are stored.
Other approaches
Recompute both sums (time O(n^2), space O(1))
For each index, sum the left slice and the right slice directly. Correct but quadratic.
Prefix sum array (time O(n), space O(n))
Build prefix[k] = nums[0] + ... + nums[k-1] and compare prefix[i] with prefix[n] - prefix[i+1]. Same speed but uses O(n) extra memory.
Common mistakes
- Including
nums[i]in one of the sides. The pivot element belongs to neither side. - Adding
nums[i]toleftbefore the comparison, which shifts every check by one index. - Returning the last balanced index instead of the first. Return as soon as a match is found.
- Assuming values are positive. Negative numbers mean the sides can balance more than once, or at index 0.
Frequently asked questions
Is this the same problem as Find Pivot Index?
Yes. Find Pivot Index (LeetCode 724) has the same definition and the same solution.
Can index 0 be the answer?
Yes. Its left sum is 0, so it is the answer whenever the rest of the array sums to 0.
Why compute the total first?
It lets you derive the right sum from the left sum in O(1), so the whole scan stays linear.