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.
Input: arr =[7,3,4,7], target =7Output: 2Explanation: 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 is2.
We want to find two non-overlapping subarrays each summing to target, with the minimum total length. We can use a sliding window to find all subarrays with sum = target, and for each ending index, keep track of the shortest subarray found so far. Then, for each subarray found, check if there is a non-overlapping subarray before it.
classSolution {
public:int minSumOfLengths(vector<int>& arr, int target) {
int n = arr.size(), ans = INT_MAX;
vector<int> minLen(n, INT_MAX);
int l =0, sum =0, best = INT_MAX;
for (int r =0; r < n; ++r) {
sum += arr[r];
while (sum > target) sum -= arr[l++];
if (sum == target) {
int currLen = r - l +1;
if (l >0&& minLen[l-1] != INT_MAX)
ans = min(ans, currLen + minLen[l-1]);
best = min(best, currLen);
}
minLen[r] = best;
}
return ans == INT_MAX ?-1: ans;
}
};
classSolution {
publicintminSumOfLengths(int[] arr, int target) {
int n = arr.length, ans = Integer.MAX_VALUE;
int[] minLen =newint[n];
Arrays.fill(minLen, Integer.MAX_VALUE);
int l = 0, sum = 0, best = Integer.MAX_VALUE;
for (int r = 0; r < n; r++) {
sum += arr[r];
while (sum > target) sum -= arr[l++];
if (sum == target) {
int currLen = r - l + 1;
if (l > 0 && minLen[l-1]!= Integer.MAX_VALUE)
ans = Math.min(ans, currLen + minLen[l-1]);
best = Math.min(best, currLen);
}
minLen[r]= best;
}
return ans == Integer.MAX_VALUE?-1 : ans;
}
}
classSolution {
funminSumOfLengths(arr: IntArray, target: Int): Int {
val n = arr.size
val minLen = IntArray(n) { Int.MAX_VALUE }
var l = 0var sum = 0var best = Int.MAX_VALUE
var ans = Int.MAX_VALUE
for (r in0 until n) {
sum += arr[r]
while (sum > target) {
sum -= arr[l++]
}
if (sum == target) {
val currLen = r - l + 1if (l > 0&& minLen[l-1] !=Int.MAX_VALUE)
ans = minOf(ans, currLen + minLen[l-1])
best = minOf(best, currLen)
}
minLen[r] = best
}
returnif (ans ==Int.MAX_VALUE) -1else ans
}
}
classSolution:
defminSumOfLengths(self, arr: list[int], target: int) -> int:
n = len(arr)
min_len = [float('inf')] * n
l =0 s =0 best = float('inf')
ans = float('inf')
for r in range(n):
s += arr[r]
while s > target:
s -= arr[l]
l +=1if s == target:
curr = r - l +1if l >0and min_len[l-1] != float('inf'):
ans = min(ans, curr + min_len[l-1])
best = min(best, curr)
min_len[r] = best
return-1if ans == float('inf') else ans
impl Solution {
pubfnmin_sum_of_lengths(arr: Vec<i32>, target: i32) -> i32 {
let n = arr.len();
letmut min_len =vec![i32::MAX; n];
let (mut l, mut s, mut best, mut ans) = (0, 0, i32::MAX, i32::MAX);
for r in0..n {
s += arr[r];
while s > target {
s -= arr[l];
l +=1;
}
if s == target {
let curr = (r - l +1) asi32;
if l >0&& min_len[l-1] !=i32::MAX {
ans = ans.min(curr + min_len[l-1]);
}
best = best.min(curr);
}
min_len[r] = best;
}
if ans ==i32::MAX { -1 } else { ans }
}
}