Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
A split of an integer array is good if:
left, mid, right respectively from left to right.left is less than or equal to the sum of the elements in mid, and the sum of the elements in mid is less than or equal to the sum of the elements in right.Given nums, an array of non-negative integers, return the number of good ways to split nums. As the number may be too large, return it modulo 109 + 7.
Example 1:
Input: nums = [1,1,1] Output: 1 Explanation: The only good way to split nums is [1] [1] [1].
Example 2:
Input: nums = [1,2,2,2,5,0] Output: 3 Explanation: There are three good ways of splitting nums: [1] [2] [2,2,5,0] [1] [2,2] [2,5,0] [1,2] [2,2] [5,0]
Example 3:
Input: nums = [3,2,1] Output: 0 Explanation: There is no good way to split nums.
Constraints:
3 <= nums.length <= 1050 <= nums[i] <= 104Problem summary: A split of an integer array is good if: The array is split into three non-empty contiguous subarrays - named left, mid, right respectively from left to right. The sum of the elements in left is less than or equal to the sum of the elements in mid, and the sum of the elements in mid is less than or equal to the sum of the elements in right. Given nums, an array of non-negative integers, return the number of good ways to split nums. As the number may be too large, return it modulo 109 + 7.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers · Binary Search
[1,1,1]
[1,2,2,2,5,0]
[3,2,1]
number-of-ways-to-divide-a-long-corridor)number-of-ways-to-split-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1712: Ways to Split Array Into Three Subarrays
class Solution {
private static final int MOD = (int) 1e9 + 7;
public int waysToSplit(int[] nums) {
int n = nums.length;
int[] s = new int[n];
s[0] = nums[0];
for (int i = 1; i < n; ++i) {
s[i] = s[i - 1] + nums[i];
}
int ans = 0;
for (int i = 0; i < n - 2; ++i) {
int j = search(s, s[i] << 1, i + 1, n - 1);
int k = search(s, ((s[n - 1] + s[i]) >> 1) + 1, j, n - 1);
ans = (ans + k - j) % MOD;
}
return ans;
}
private int search(int[] s, int x, int left, int right) {
while (left < right) {
int mid = (left + right) >> 1;
if (s[mid] >= x) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
}
// Accepted solution for LeetCode #1712: Ways to Split Array Into Three Subarrays
func waysToSplit(nums []int) (ans int) {
const mod int = 1e9 + 7
n := len(nums)
s := make([]int, n)
s[0] = nums[0]
for i := 1; i < n; i++ {
s[i] = s[i-1] + nums[i]
}
for i := 0; i < n-2; i++ {
j := sort.Search(n-1, func(h int) bool { return h > i && s[h] >= (s[i]<<1) })
k := sort.Search(n-1, func(h int) bool { return h >= j && s[h] > (s[n-1]+s[i])>>1 })
ans = (ans + k - j) % mod
}
return
}
# Accepted solution for LeetCode #1712: Ways to Split Array Into Three Subarrays
class Solution:
def waysToSplit(self, nums: List[int]) -> int:
mod = 10**9 + 7
s = list(accumulate(nums))
ans, n = 0, len(nums)
for i in range(n - 2):
j = bisect_left(s, s[i] << 1, i + 1, n - 1)
k = bisect_right(s, (s[-1] + s[i]) >> 1, j, n - 1)
ans += k - j
return ans % mod
// Accepted solution for LeetCode #1712: Ways to Split Array Into Three Subarrays
struct Solution;
const MOD: i64 = 1_000_000_007;
impl Solution {
fn ways_to_split(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut prefix: Vec<i32> = vec![];
let mut prev = 0;
let mut res = 0;
for i in 0..n {
prev += nums[i];
prefix.push(prev);
}
let total = prev;
let mut mid_start = 0;
let mut mid_end = 0;
for i in 0..n {
let left = prefix[i];
mid_start = mid_start.max(i + 1);
while mid_start < n && prefix[mid_start] - left < left {
mid_start += 1;
}
while mid_end + 1 < n && total - prefix[mid_end] >= prefix[mid_end] - left {
mid_end += 1;
}
if mid_end >= mid_start {
res += (mid_end - mid_start) as i64;
res %= MOD;
}
}
res as i32
}
}
#[test]
fn test() {
let nums = vec![1, 1, 1];
let res = 1;
assert_eq!(Solution::ways_to_split(nums), res);
let nums = vec![1, 2, 2, 2, 5, 0];
let res = 3;
assert_eq!(Solution::ways_to_split(nums), res);
let nums = vec![3, 2, 1];
let res = 0;
assert_eq!(Solution::ways_to_split(nums), res);
let nums = vec![0, 3, 3];
let res = 1;
assert_eq!(Solution::ways_to_split(nums), res);
}
// Accepted solution for LeetCode #1712: Ways to Split Array Into Three Subarrays
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1712: Ways to Split Array Into Three Subarrays
// class Solution {
// private static final int MOD = (int) 1e9 + 7;
//
// public int waysToSplit(int[] nums) {
// int n = nums.length;
// int[] s = new int[n];
// s[0] = nums[0];
// for (int i = 1; i < n; ++i) {
// s[i] = s[i - 1] + nums[i];
// }
// int ans = 0;
// for (int i = 0; i < n - 2; ++i) {
// int j = search(s, s[i] << 1, i + 1, n - 1);
// int k = search(s, ((s[n - 1] + s[i]) >> 1) + 1, j, n - 1);
// ans = (ans + k - j) % MOD;
// }
// return ans;
// }
//
// private int search(int[] s, int x, int left, int right) {
// while (left < right) {
// int mid = (left + right) >> 1;
// if (s[mid] >= x) {
// right = mid;
// } else {
// left = mid + 1;
// }
// }
// return left;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.