FastPrepEqual Sum Split After One Removal
Problem · Array

Equal Sum Split After One Removal

Learn this problem
MediumGoogle logoGoogleINTERNPHONE SCREEN
See Google hiring insights

Problem statement

Given an array nums of positive integers, remove exactly one element while preserving the relative order of all remaining elements.

Return true if the remaining sequence can be split into two non-empty contiguous subarrays with equal sums. Otherwise, return false.

Removal and split rules

  • You may choose any one index to remove.
  • After the removal, every remaining element must belong to exactly one of the two subarrays.
  • Both subarrays must be contiguous in the remaining sequence.

Function

canSplitAfterRemoval(nums: int[]) → boolean

Examples

Example 1

nums = [1,2,3,3]return = true

Remove the last 3. The remaining sequence is [1,2,3], which splits into [1,2] and [3]. Both sums are 3.

Example 2

nums = [1,2,5]return = false

Every removal leaves two unequal single-element subarrays, so no valid split exists.

Example 3

nums = [2,1,1,2]return = true

Remove the first 2. The remaining sequence [1,1,2] splits into [1,1] and [2], each with sum 2.

Constraints

  • 3 <= nums.length <= 2 * 10^5
  • 1 <= nums[i] <= 10^9

More Google problems

drafts saved locally
public boolean canSplitAfterRemoval(int[] nums) {
  // write your code here
}
nums[1,2,3,3]
expectedtrue
checking account