#1477
Find Two Non-overlapping Sub-arrays Each With Target Sum
expert · 1045 · lc medium +32 · verified · 36.8% accepted · 1,767 likes · top 15%
Description
Given an integer array arr and a value target, locate two non-overlapping contiguous subarrays that each sum to target. Among all valid pairs, return the minimum possible combined length of the two subarrays. If no such pair exists, return -1.
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.
Code
1
2
3