FastPrepDelete K Values to Balance Index Sums
Problem · Array

Delete K Values to Balance Index Sums

Learn this problem
MediumByteDance logoByteDanceFULLTIMEPHONE SCREEN

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

Examples

Example 1

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

Deleting 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 = -1

No single-value deletion balances the two index-parity sums.

Constraints

  • 1 <= nums.length <= 100000
  • 1 <= k <= nums.length
  • -10^9 <= nums[i] <= 10^9

More ByteDance problems

drafts saved locally
public int firstBalancedBlock(int[] nums, int k) {
  // Write your code here.
}
nums[2,1,6,4]
k1
expected1
checking account