Formatted question description: https://leetcode.ca/all/1477.html

# 1477. Find Two Non-overlapping Sub-arrays Each With Target Sum

Medium

## Description

Given an array of integers arr and an integer target.

You have to find two non-overlapping sub-arrays of arr each with 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 ( and ). 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 (, [3,4] and ), 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.

Example 4:

Input: arr = [5,5,4,4,5], target = 3

Output: -1

Explanation: We cannot find a sub-array of sum = 3.

Example 5:

Input: arr = [3,1,1,1,5,1,2,1], target = 3

Output: 3

Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.

Constraints:

• 1 <= arr.length <= 10^5
• 1 <= arr[i] <= 1000
• 1 <= target <= 10^8

## Solution

First, use the idea of sliding window to obtain all subarrays that have sum equal to target, and use a list to store each subarray’s start index and end index. Sort the subarrays according to their lengths in ascending order. Then find the two shortest arrays that do not overlap, and return the sum of their lengths.

Java

class Solution {
public int minSumOfLengths(int[] arr, int target) {
List<int[]> indices = new ArrayList<int[]>();
int length = arr.length;
int sum = 0;
int start = 0, end = 0;
while (end < length) {
sum += arr[end];
while (sum > target) {
sum -= arr[start];
start++;
}
if (sum == target) {
sum -= arr[start];
start++;
}
end++;
}
int size = indices.size();
if (size <= 1)
return -1;
Collections.sort(indices, new Comparator<int[]>() {
public int compare(int[] array1, int[] array2) {
if (array1 - array1 != array2 - array2)
return (array1 - array1) - (array2 - array2);
else
return array1 - array2;
}
});
int[] array0 = indices.get(0);
int length1 = array0 - array0 + 1;
int length2 = Integer.MAX_VALUE;
for (int i = 1; i < size; i++) {
int[] array = indices.get(i);
int curLength = array - array + 1;
if (array > array0 || array < array0) {
length2 = curLength;
return length1 + length2;
}
if (curLength > length1) {
for (int j = 0; j < i; j++) {
int[] prevArray = indices.get(j);
int prevLength = prevArray - prevArray + 1;
if (prevLength == length1 && (array > prevArray || array < prevArray)) {
length2 = curLength;
return length1 + length2;
}
}
}
}
return -1;
}
}