Problem · Array
Equal Sum Split After One Removal
Learn this problemProblem 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[]) → booleanExamples
Example 1
nums = [1,2,3,3]return = trueRemove 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 = falseEvery removal leaves two unequal single-element subarrays, so no valid split exists.
Example 3
nums = [2,1,1,2]return = trueRemove 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^51 <= nums[i] <= 10^9