Problem · Array

Good Ways to Split an Array

Learn this problem
MediumSalesforcePHONE SCREEN
See Salesforce hiring insights

Problem statement

You are given an array of non-negative integers nums. Split it into three non-empty contiguous subarrays A1, A2, and A3.

Let S1, S2, and S3 be their sums. Count the number of splits such that S2 <= S1 + S3. Return the answer modulo 10^9 + 7.

Function

countGoodSplits(nums: int[]) → int

Examples

Example 1

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

The valid split points are after [1] | [2] | [3,4], [1] | [2,3] | [4], and [1,2] | [3] | [4]. All three satisfy S2 <= S1 + S3.

The source shared the rule but did not include this exact sample. FastPrep added this small example so the behavior can be checked directly.

More Salesforce problems

drafts saved locally
public int countGoodSplits(int[] nums) {
  // write your code here
}
nums[1,2,3,4]
expected3
checking account