Quant Memo
Coding/●●●●

Two non-overlapping subarrays of any length

Given an array of signed values (daily PnL that can be negative), choose two non-overlapping non-empty contiguous subarrays to maximize their combined sum. The subarrays can be any lengths.

pnl = [3, -2, 5, -1, 4, -8, 6]
-> 15          # block A = [3, -2, 5, -1, 4] = 9, block B = [6] = 6

Return the maximum combined sum. Aim for O(n)O(n).

Show a hint

If you knew the gap between the two blocks, one block lives entirely on the left and the other entirely on the right. Precompute, for every split point, the best single subarray on each side.

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions