Leetcode Problem 1991. Find the Middle Index in Array

1991. Find the Middle Index in Array

Leetcode Solutions

Prefix Sum Approach

  1. Initialize two arrays, leftSum and rightSum, of the same length as the input array nums.
  2. Populate leftSum such that leftSum[i] contains the sum of elements from nums[0] to nums[i].
  3. Populate rightSum in a similar manner but in reverse, starting from the end of the array.
  4. Iterate through the array and compare leftSum[i-1] and rightSum[i+1] for each index i.
  5. If they are equal, return the current index i as the middleIndex.
  6. If no such index is found, return -1.
UML Thumbnail

Single Pass with Running Totals

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...