Problem

First Balanced Removal Index

Learn this problem
Oracle logoOracleFULLTIMEOA

Problem 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[]) → int

Examples

Example 1

nums = [2, 1, 6, 4]return = 1

Remove 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 = 0

Removing 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

More Oracle problems

drafts saved locally
public int firstBalancedRemovalIndex(int[] nums) {
  // write your code here
}
nums[2, 1, 6, 4]
expected1
checking account