Skip to content

Practice Find Two Non-overlapping Sub-arrays Each With Target Sum

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1477
LeetCode Problem

Find Two Non-overlapping Sub-arrays Each With Target Sum

Problem Statement

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.

Source: Find Two Non-overlapping Sub-arrays Each With Target Sum on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
arr = [3,2,2,4,3], target = 3
Output
2

# Example 2

Input
arr = [7,3,4,7], target = 7
Output
2

# Example 3

Input
arr = [4,3,2,6,2,3,4], target = 6
Output
-1

Constraints

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

Related LeetCode Problems

Practice more problems similar to Find Two Non-overlapping Sub-arrays Each With Target Sum.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.