Dodomeki
Sign in

52. The First Balance Index

Easy

An index in a list of numbers is a balance index when the sum of the elements before it equals the sum of the elements after it. The element at the index itself is excluded from both sums.

Given nums, return the smallest balance index, using zero-based indexing. If no such index exists, return -1.

Example 1
Input: nums = [2, 3, -1, 8, 4]
Output: 3
Explanation: The sum before index 3 is 4, and the sum after it is also 4.
Example 2
Input: nums = [1, 2, 3]
Output: -1
Explanation: No index splits the list into two parts with equal sums.
Example 3
Input: nums = [0, 0]
Output: 0
Explanation: At index 0, both exclusive sums are zero.
Constraints
  • 0 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
Prefix sumsArrays
Caso 1
nums =
[2,3,-1,8,4]
Expected =
3
Caso 2
nums =
[1,2,3]
Expected =
-1
Caso 3
nums =
[0,0]
Expected =
0

Run checks these 3 cases. Submit runs all 11, including hidden ones.