First Balanced Removal Index
Updated
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[]) → 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
More Oracle Online Assessment questions
- Find Circle NumberOA · Seen Oct 2024
- Create Lexicographically Largest PermutationOA · Seen Sep 2024
- Array Reduction 1OA · Seen Feb 2024
- Balancing ParenthesesOA · Seen Feb 2024
- Merge 2 ArraysOA · Seen Feb 2024
- Last and Second-LastOA · Seen Feb 2024
- Minimum Difference SumOA · Seen Feb 2024
- Powers of 2OA · Seen Feb 2024
- Adding Two NumbersOA · Seen Feb 2024
- REST API: Discounted PriceOA · Seen Feb 2024