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.
Given an integer array nums and two integers firstLen and secondLen, return the maximum sum of elements in two non-overlapping subarrays with lengths firstLen and secondLen.
The array with length firstLen could occur before or after the array with length secondLen, but they have to be non-overlapping.
A subarray is a contiguous part of an array.
Example 1:
Input: nums = [0,6,5,2,2,5,1,9,4], firstLen = 1, secondLen = 2 Output: 20 Explanation: One choice of subarrays is [9] with length 1, and [6,5] with length 2.
Example 2:
Input: nums = [3,8,1,3,2,1,8,9,0], firstLen = 3, secondLen = 2 Output: 29 Explanation: One choice of subarrays is [3,8,1] with length 3, and [8,9] with length 2.
Example 3:
Input: nums = [2,1,5,6,0,9,5,0,3,8], firstLen = 4, secondLen = 3 Output: 31 Explanation: One choice of subarrays is [5,6,0,9] with length 4, and [0,3,8] with length 3.
Constraints:
1 <= firstLen, secondLen <= 10002 <= firstLen + secondLen <= 1000firstLen + secondLen <= nums.length <= 10000 <= nums[i] <= 1000Problem summary: Given an integer array nums and two integers firstLen and secondLen, return the maximum sum of elements in two non-overlapping subarrays with lengths firstLen and secondLen. The array with length firstLen could occur before or after the array with length secondLen, but they have to be non-overlapping. A subarray is a contiguous part of an array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming · Sliding Window
[0,6,5,2,2,5,1,9,4] 1 2
[3,8,1,3,2,1,8,9,0] 3 2
[2,1,5,6,0,9,5,0,3,8] 4 3
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1031: Maximum Sum of Two Non-Overlapping Subarrays
class Solution {
public int maxSumTwoNoOverlap(int[] nums, int firstLen, int secondLen) {
int n = nums.length;
int[] s = new int[n + 1];
for (int i = 0; i < n; ++i) {
s[i + 1] = s[i] + nums[i];
}
int ans = 0;
for (int i = firstLen, t = 0; i + secondLen - 1 < n; ++i) {
t = Math.max(t, s[i] - s[i - firstLen]);
ans = Math.max(ans, t + s[i + secondLen] - s[i]);
}
for (int i = secondLen, t = 0; i + firstLen - 1 < n; ++i) {
t = Math.max(t, s[i] - s[i - secondLen]);
ans = Math.max(ans, t + s[i + firstLen] - s[i]);
}
return ans;
}
}
// Accepted solution for LeetCode #1031: Maximum Sum of Two Non-Overlapping Subarrays
func maxSumTwoNoOverlap(nums []int, firstLen int, secondLen int) (ans int) {
n := len(nums)
s := make([]int, n+1)
for i, x := range nums {
s[i+1] = s[i] + x
}
for i, t := firstLen, 0; i+secondLen-1 < n; i++ {
t = max(t, s[i]-s[i-firstLen])
ans = max(ans, t+s[i+secondLen]-s[i])
}
for i, t := secondLen, 0; i+firstLen-1 < n; i++ {
t = max(t, s[i]-s[i-secondLen])
ans = max(ans, t+s[i+firstLen]-s[i])
}
return
}
# Accepted solution for LeetCode #1031: Maximum Sum of Two Non-Overlapping Subarrays
class Solution:
def maxSumTwoNoOverlap(self, nums: List[int], firstLen: int, secondLen: int) -> int:
n = len(nums)
s = list(accumulate(nums, initial=0))
ans = t = 0
i = firstLen
while i + secondLen - 1 < n:
t = max(t, s[i] - s[i - firstLen])
ans = max(ans, t + s[i + secondLen] - s[i])
i += 1
t = 0
i = secondLen
while i + firstLen - 1 < n:
t = max(t, s[i] - s[i - secondLen])
ans = max(ans, t + s[i + firstLen] - s[i])
i += 1
return ans
// Accepted solution for LeetCode #1031: Maximum Sum of Two Non-Overlapping Subarrays
struct Solution;
use std::i32;
impl Solution {
fn max_sum_two_no_overlap(mut a: Vec<i32>, l: i32, m: i32) -> i32 {
let n = a.len();
let l = l as usize;
let m = m as usize;
for i in 1..n {
a[i] += a[i - 1];
}
let mut res = a[l + m - 1];
let mut max_l = a[l - 1];
let mut max_m = a[m - 1];
for i in l + m..n {
max_l = i32::max(a[i - m] - a[i - m - l], max_l);
max_m = i32::max(a[i - l] - a[i - l - m], max_m);
let last_l = a[i] - a[i - l];
let last_m = a[i] - a[i - m];
res = i32::max(i32::max(max_m + last_l, max_l + last_m), res);
}
res
}
}
#[test]
fn test() {
let a = vec![0, 6, 5, 2, 2, 5, 1, 9, 4];
let l = 1;
let m = 2;
let res = 20;
assert_eq!(Solution::max_sum_two_no_overlap(a, l, m), res);
let a = vec![3, 8, 1, 3, 2, 1, 8, 9, 0];
let l = 3;
let m = 2;
let res = 29;
assert_eq!(Solution::max_sum_two_no_overlap(a, l, m), res);
let a = vec![2, 1, 5, 6, 0, 9, 5, 0, 3, 8];
let l = 4;
let m = 3;
let res = 31;
assert_eq!(Solution::max_sum_two_no_overlap(a, l, m), res);
}
// Accepted solution for LeetCode #1031: Maximum Sum of Two Non-Overlapping Subarrays
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1031: Maximum Sum of Two Non-Overlapping Subarrays
// class Solution {
// public int maxSumTwoNoOverlap(int[] nums, int firstLen, int secondLen) {
// int n = nums.length;
// int[] s = new int[n + 1];
// for (int i = 0; i < n; ++i) {
// s[i + 1] = s[i] + nums[i];
// }
// int ans = 0;
// for (int i = firstLen, t = 0; i + secondLen - 1 < n; ++i) {
// t = Math.max(t, s[i] - s[i - firstLen]);
// ans = Math.max(ans, t + s[i + secondLen] - s[i]);
// }
// for (int i = secondLen, t = 0; i + firstLen - 1 < n; ++i) {
// t = Math.max(t, s[i] - s[i - secondLen]);
// ans = Math.max(ans, t + s[i + firstLen] - s[i]);
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
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: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.