{"date":"2026-09-17","link":"/problems/find-two-non-overlapping-sub-arrays-each-with-target-sum/","question":{"questionId":"1573","questionFrontendId":"1477","title":"Find Two Non-overlapping Sub-arrays Each With Target Sum","titleSlug":"find-two-non-overlapping-sub-arrays-each-with-target-sum","translatedTitle":null,"difficulty":"Medium","acRate":42.05033246359734,"topicTags":[{"name":"Array","slug":"array","nameTranslated":null},{"name":"Hash Table","slug":"hash-table","nameTranslated":null},{"name":"Binary Search","slug":"binary-search","nameTranslated":null},{"name":"Dynamic Programming","slug":"dynamic-programming","nameTranslated":null},{"name":"Sliding Window","slug":"sliding-window","nameTranslated":null}],"content":"<p>You are given an array of integers <code>arr</code> and an integer <code>target</code>.</p>\n\n<p>You have to find <strong>two non-overlapping sub-arrays</strong> of <code>arr</code> each with a sum equal <code>target</code>. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is <strong>minimum</strong>.</p>\n\n<p>Return <em>the minimum sum of the lengths</em> of the two required sub-arrays, or return <code>-1</code> if you cannot find such two sub-arrays.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [3,2,2,4,3], target = 3\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [7,3,4,7], target = 7\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> 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.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [4,3,2,6,2,3,4], target = 6\n<strong>Output:</strong> -1\n<strong>Explanation:</strong> We have only one sub-array of sum = 6.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= arr.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= arr[i] &lt;= 1000</code></li>\n\t<li><code>1 &lt;= target &lt;= 10<sup>8</sup></code></li>\n</ul>\n"}}