Skip to main content
Back to problems
Leetcode
Easy
Arrays
Math
Find The Middle Index In Array

Find an index where the sum of elements on the left equals the sum of elements on the right.

Acceptance 100%
Also Available On
Other platform versions and source mappings for the same problem.
Problem Statement

Problem

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.

Intuition

You are looking for a position that balances the array into two parts with equal sum, ignoring the element at the index itself.

Input Format

  • One integer array nums of length n.
  • nums[i] may be positive, negative, or zero.

Output Format

  • Return the leftmost index i such that:
    • sum(nums[0..i-1]) == sum(nums[i+1..n-1])
  • If no such index exists, return -1.

Constraints

  • 1 <= n
  • The array can contain negative values and zeros.
  • Use 0-based indexing.
Examples
Sample cases returned by the problem API.

Example 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.

Show 1 more example

Example 3

Input

nums = [2, 5]

Output

-1

Explanation

No index has equal sums on both sides.

Premium problem context

Unlock deeper context for this problem

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.