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.
You are given an integer array nums.
Split the array into exactly two subarrays, left and right, such that left is strictly increasing and right is strictly decreasing.
Return the minimum possible absolute difference between the sums of left and right. If no valid split exists, return -1.
Example 1:
Input: nums = [1,3,2]
Output: 2
Explanation:
i |
left |
right |
Validity | left sum |
right sum |
Absolute difference |
|---|---|---|---|---|---|---|
| 0 | [1] | [3, 2] | Yes | 1 | 5 | |1 - 5| = 4 |
| 1 | [1, 3] | [2] | Yes | 4 | 2 | |4 - 2| = 2 |
Thus, the minimum absolute difference is 2.
Example 2:
Input: nums = [1,2,4,3]
Output: 4
Explanation:
i |
left |
right |
Validity | left sum |
right sum |
Absolute difference |
|---|---|---|---|---|---|---|
| 0 | [1] | [2, 4, 3] | No | 1 | 9 | - |
| 1 | [1, 2] | [4, 3] | Yes | 3 | 7 | |3 - 7| = 4 |
| 2 | [1, 2, 4] | [3] | Yes | 7 | 3 | |7 - 3| = 4 |
Thus, the minimum absolute difference is 4.
Example 3:
Input: nums = [3,1,2]
Output: -1
Explanation:
No valid split exists, so the answer is -1.
Constraints:
2 <= nums.length <= 1051 <= nums[i] <= 105Problem summary: You are given an integer array nums. Split the array into exactly two subarrays, left and right, such that left is strictly increasing and right is strictly decreasing. Return the minimum possible absolute difference between the sums of left and right. If no valid split exists, return -1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,3,2]
[1,2,4,3]
[3,1,2]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3698: Split Array With Minimum Difference
class Solution {
public long splitArray(int[] nums) {
int n = nums.length;
long[] s = new long[n];
s[0] = nums[0];
boolean[] f = new boolean[n];
Arrays.fill(f, true);
boolean[] g = new boolean[n];
Arrays.fill(g, true);
for (int i = 1; i < n; ++i) {
s[i] = s[i - 1] + nums[i];
f[i] = f[i - 1];
if (nums[i] <= nums[i - 1]) {
f[i] = false;
}
}
for (int i = n - 2; i >= 0; --i) {
g[i] = g[i + 1];
if (nums[i] <= nums[i + 1]) {
g[i] = false;
}
}
final long inf = Long.MAX_VALUE;
long ans = inf;
for (int i = 0; i < n - 1; ++i) {
if (f[i] && g[i + 1]) {
long s1 = s[i];
long s2 = s[n - 1] - s[i];
ans = Math.min(ans, Math.abs(s1 - s2));
}
}
return ans < inf ? ans : -1;
}
}
// Accepted solution for LeetCode #3698: Split Array With Minimum Difference
func splitArray(nums []int) int64 {
n := len(nums)
s := make([]int64, n)
f := make([]bool, n)
g := make([]bool, n)
for i := range f {
f[i] = true
g[i] = true
}
s[0] = int64(nums[0])
for i := 1; i < n; i++ {
s[i] = s[i-1] + int64(nums[i])
f[i] = f[i-1]
if nums[i] <= nums[i-1] {
f[i] = false
}
}
for i := n - 2; i >= 0; i-- {
g[i] = g[i+1]
if nums[i] <= nums[i+1] {
g[i] = false
}
}
const inf = int64(^uint64(0) >> 1)
ans := inf
for i := 0; i < n-1; i++ {
if f[i] && g[i+1] {
s1 := s[i]
s2 := s[n-1] - s[i]
ans = min(ans, abs(s1-s2))
}
}
if ans < inf {
return ans
}
return -1
}
func abs(x int64) int64 {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #3698: Split Array With Minimum Difference
class Solution:
def splitArray(self, nums: List[int]) -> int:
s = list(accumulate(nums))
n = len(nums)
f = [True] * n
for i in range(1, n):
f[i] = f[i - 1]
if nums[i] <= nums[i - 1]:
f[i] = False
g = [True] * n
for i in range(n - 2, -1, -1):
g[i] = g[i + 1]
if nums[i] <= nums[i + 1]:
g[i] = False
ans = inf
for i in range(n - 1):
if f[i] and g[i + 1]:
s1 = s[i]
s2 = s[n - 1] - s[i]
ans = min(ans, abs(s1 - s2))
return ans if ans < inf else -1
// Accepted solution for LeetCode #3698: Split Array With Minimum Difference
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3698: Split Array With Minimum Difference
// class Solution {
// public long splitArray(int[] nums) {
// int n = nums.length;
// long[] s = new long[n];
// s[0] = nums[0];
// boolean[] f = new boolean[n];
// Arrays.fill(f, true);
// boolean[] g = new boolean[n];
// Arrays.fill(g, true);
// for (int i = 1; i < n; ++i) {
// s[i] = s[i - 1] + nums[i];
// f[i] = f[i - 1];
// if (nums[i] <= nums[i - 1]) {
// f[i] = false;
// }
// }
// for (int i = n - 2; i >= 0; --i) {
// g[i] = g[i + 1];
// if (nums[i] <= nums[i + 1]) {
// g[i] = false;
// }
// }
// final long inf = Long.MAX_VALUE;
// long ans = inf;
// for (int i = 0; i < n - 1; ++i) {
// if (f[i] && g[i + 1]) {
// long s1 = s[i];
// long s2 = s[n - 1] - s[i];
// ans = Math.min(ans, Math.abs(s1 - s2));
// }
// }
// return ans < inf ? ans : -1;
// }
// }
// Accepted solution for LeetCode #3698: Split Array With Minimum Difference
function splitArray(nums: number[]): number {
const n = nums.length;
const s: number[] = Array(n);
const f: boolean[] = Array(n).fill(true);
const g: boolean[] = Array(n).fill(true);
s[0] = nums[0];
for (let i = 1; i < n; ++i) {
s[i] = s[i - 1] + nums[i];
f[i] = f[i - 1];
if (nums[i] <= nums[i - 1]) {
f[i] = false;
}
}
for (let i = n - 2; i >= 0; --i) {
g[i] = g[i + 1];
if (nums[i] <= nums[i + 1]) {
g[i] = false;
}
}
const INF = Number.MAX_SAFE_INTEGER;
let ans = INF;
for (let i = 0; i < n - 1; ++i) {
if (f[i] && g[i + 1]) {
const s1 = s[i];
const s2 = s[n - 1] - s[i];
ans = Math.min(ans, Math.abs(s1 - s2));
}
}
return ans < INF ? ans : -1;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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.