FastPrepFind an Equal-Sum Split

Find an Equal-Sum Split

Meta logoMeta● EasyINTERNPHONE SCREEN
Learn

Problem statement

Given an integer array nums, determine whether one boundary can divide it into two contiguous, nonempty subarrays with equal sums. Every element must belong to exactly one of the two subarrays.

Return true when such a boundary exists. Otherwise, return false.

Function

canSplitEqualSum(nums: int[]) → boolean

Examples

Example 1

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

Split after the first three elements. The sums of [1,2,3] and [6] are both 6.

Example 2

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

Neither valid boundary gives equal left and right sums.

Example 3

nums = [0,0,0]return = true

Splitting after the first element gives two nonempty parts whose sums are both 0.

Example 4

nums = [2,-2,5,0,5]return = true

The contiguous parts [2,-2,5] and [0,5] both sum to 5.

Constraints

  • 2 <= nums.length <= 2 * 10^5.
  • -10^9 <= nums[i] <= 10^9.
  • Every prefix sum and the total sum fit in a signed 64-bit integer.

More Meta problems

See Meta hiring insights
public boolean canSplitEqualSum(int[] nums) {
    // write your code here
}
nums[1,2,3,6]
expectedtrue
Checking account…