Find Two Non-overlapping Sub-arrays Each With Target Sum Problem
Description
LeetCode Problem 1477.
You are given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with a sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Constraints:
1 <= arr.length <= 10^51 <= arr[i] <= 10001 <= target <= 10^8
Solution
Approach: Prefix Sums + Best Sub-array Seen So Far
Fix the sub-array that comes second (the one further right). The other one must lie completely before it, and among those we only care about the shortest one. So, as we scan from left to right, we keep:
best[i]: the length of the shortest sub-array with sumtargetthat lies within the firstielements (or "none").
How do we find the sub-arrays that end at the current position? All values are positive, so the prefix sums strictly increase and each value appears at most once. Store every prefix sum in a hash map prefix sum -> number of elements it covers. If the running sum after i elements is s and s - target was seen after j elements, then arr[j..i-1] sums to target, and it is the only such sub-array ending here.
When that happens:
- Pair it with the best sub-array that ends at or before position
j: the candidate answer isbest[j] + (i - j). Because the left one ends byjand the right one starts afterj, they never overlap. - Update
best[i] = min(best[i - 1], i - j).
Otherwise best[i] = best[i - 1].
The tricky case is overlap: in [1,1,1,2,2,2,4,4] with target = 6, the two shortest sub-arrays ([2,2,2] and [2,4]) share an element, so they cannot both be used. Using best[j] (not the global best) is what rules this out. Use "infinity" as a value larger than n so that a missing pair stays above n, and return -1 in that case.
Complexity: O(n) time and O(n) extra space for the map and the best array.
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n = len(arr)
INF = n + 1 # any length sum above n means "not found"
first_at = {0: 0} # prefix sum -> number of elements it covers
best = [INF] * (n + 1) # best[i]: shortest target sub-array within arr[:i]
ans = 2 * INF
s = 0
for i, v in enumerate(arr, 1):
s += v
best[i] = best[i - 1]
j = first_at.get(s - target)
if j is not None: # arr[j:i] sums to target
best[i] = min(best[i], i - j)
ans = min(ans, best[j] + i - j) # pair it with the best one ending by j
first_at[s] = i
return -1 if ans > n else ans