Problem
First Balanced Removal Index
Learn this problemProblem statement
You are given an integer array nums. Choose exactly one index and remove the element at that index. After removal, the remaining elements shift left to close the gap.
An index is balanced if, after removing that element, the sum of values at even positions is equal to the sum of values at odd positions.
Return the first balanced index. If no such index exists, return -1.
Positions are zero-indexed.
Function
firstBalancedRemovalIndex(nums: int[]) → intExamples
Example 1
nums = [2, 1, 6, 4]return = 1Remove nums[1]. The remaining array is [2,6,4]. The even-position sum is 2 + 4 = 6, and the odd-position sum is 6.
Example 2
nums = [1, 1, 1]return = 0Removing index 0 leaves [1,1], whose even and odd sums are both 1. This is the first valid index.
Constraints
1 <= nums.length <= 105-106 <= nums[i] <= 106