Problem · Array
Delete K Values to Balance Index Sums
Learn this problemProblem statement
You are given an integer array nums and an integer k. Delete exactly one contiguous block of k values.
After deletion, the remaining values close the gap and receive new zero-based indices. Find the smallest start index of a block whose deletion makes the sum at even indices equal the sum at odd indices.
Return that smallest start index, or -1 when no block works.
Function
firstBalancedBlock(nums: int[], k: int) → intExamples
Example 1
nums = [2,1,6,4]k = 1return = 1Deleting nums[1] leaves [2,6,4]. Its even-index sum is 2 + 4 = 6, equal to its odd-index sum 6.
Example 2
nums = [1,2,3]k = 1return = -1No single-value deletion balances the two index-parity sums.
Constraints
1 <= nums.length <= 1000001 <= k <= nums.length-10^9 <= nums[i] <= 10^9