
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find two non-overlapping sub-arrays each with target sum using Python
Suppose we have an array of arr and another value target. We have to find two non-overlapping sub-arrays of arr where each has sum equal to target. If there are multiple answers, then we have to find an answer where the sum of the lengths of the two sub-arrays is smallest. We have to find the minimum sum of the lengths of the two required sub-arrays, if there is no such subarray then return -1.
So, if the input is like arr = [5,2,6,3,2,5] target = 5, then the output will be 2 there are three subarrays with sum 5, they are [5], [3,2] and [5], now among them only two have smallest size.
To solve this, we will follow these steps −
ans := infinity
best := make an array whose size is same as arr and fill with infinity
prefix := 0
latest := a map where store -1 for key 0
for each index i and value x of arr, do
prefix := prefix + x
if (prefix - target) is present in latest, then
ii := latest[prefix - target]
if ii >= 0, then
ans := minimum of ans and (i - ii + best[ii])
best[i] := i - ii
if i is non-zero, then
latest[prefix] := i
return ans if ans < 999999 otherwise -1
Let us see the following implementation to get better understanding −
Example
def solve(arr, target): ans = 999999 best = [999999]*len(arr) prefix = 0 latest = {0: -1} for i, x in enumerate(arr): prefix += x if prefix - target in latest: ii = latest[prefix - target] if ii >= 0: ans = min(ans, i - ii + best[ii]) best[i] = i - ii if i: best[i] = min(best[i-1], best[i]) latest[prefix] = i return ans if ans < 999999 else -1 arr = [5,2,6,3,2,5] target = 5 print(solve(arr, target))
Input
[5,2,6,3,2,5], 5
Output
2
- Related Articles
- Program to find maximum number of non-overlapping subarrays with sum equals target using Python
- Program to find number of sub-arrays with odd sum using Python
- Program to find maximum sum of two non-overlapping sublists in Python
- Find the overlapping sum of two arrays using C++
- Program to find largest sum of 3 non-overlapping sublists of same sum in Python
- Find sub-arrays from given two arrays such that they have equal sum in Python
- Program to find sum of k non-overlapping sublists whose sum is maximum in C++
- Program to find maximum number of non-overlapping substrings in Python
- Maximum Sum of Two Non-Overlapping Subarrays in C++
- Maximum OR sum of sub-arrays of two different arrays in C++
- Maximum sum two non-overlapping subarrays of given size in C++
- Program to find sum of two numbers which are less than the target in Python
- Python program for sum of consecutive numbers with overlapping in lists
- Program to find number of sets of k-non-overlapping line segments in Python
- Program to find minimum operations needed to make two arrays sum equal in Python
