- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find the sum of the lengths of two nonoverlapping sublists whose sum is given in Python

Suppose we have a list of numbers called nums and another value k, we have to find two nonoverlapping sublists in nums whose sum is k, and we have to find the sum of their lengths. When there are more than two possible sublists, we have to find the sum of the lengths of the two smallest sublists. If we cannot find the answer, return −1.

So, if the input is like nums = [7, 10, −2, −1, 4, 3] k = 7, then the output will be 3, as we pick the sublists like [7] and [4, 3]. We did not pick [10, −2, −1] as this is longer.

To solve this, we will follow these steps −

N := size of A

prefix := of size N, and fill with infinity

last := a map with value −1 for key 0 {0: −1}

s := 0

for i in range 0 to N, do

s := s + A[i]

prefix[i] := i − last[s − target], if not found set −infinity

last[s] := i

for i in range 1 to N, do

prefix[i] := minimum of prefix[i], prefix[i − 1]

suffix := of size N, and fill with infinity

last := a map with value N for key 0 {0: N}

s := 0

for i in range N − 1 to −1, decrease by 1, do

s := s + A[i]

suffix[i] := last[s − target] (if not found set infinity) − i

last[s] := i

for i in range N − 2 to −1, decrease by 1, do

suffix[i] := minimum of suffix[i] and suffix[i + 1]

ans := minimum of prefix[i] + suffix[i + 1] for each i in range 0 to N − 1

return ans if ans < infinity otherwise −1

Let us see the following implementation to get better understanding −

## Example

class Solution: def solve(self, A, target): INF = float("inf") N = len(A) prefix = [INF] * N last = {0: −1} s = 0 for i in range(N): s += A[i] prefix[i] = i − last.get(s − target, −INF) last[s] = i for i in range(1, N): prefix[i] = min(prefix[i], prefix[i − 1]) suffix = [INF] * N last = {0: N} s = 0 for i in range(N − 1, −1, −1): s += A[i] suffix[i] = last.get(s − target, INF) − i last[s] = i for i in range(N − 2, −1, −1): suffix[i] = min(suffix[i], suffix[i + 1]) ans = min(prefix[i] + suffix[i + 1] for i in range(N − 1)) return ans if ans < INF else −1 ob = Solution() nums = [7, 10, −2, −1, 4, 3] k = 7 print(ob.solve(nums, k))

## Input

[7, 10, −2, −1, 4, 3], 7

## Output

3

- Related Questions & Answers
- Program to find number of sublists whose sum is given target in python
- Program to find sum of the sum of all contiguous sublists in Python
- Program to find sum of k non-overlapping sublists whose sum is maximum in C++
- Program to find maximum sum of two non-overlapping sublists in Python
- Program to find product of few numbers whose sum is given in Python
- Program to find largest sum of 3 non-overlapping sublists of same sum in Python
- Program to find sum of rectangle whose sum at most k in Python
- Program to find length of longest sublist whose sum is 0 in Python
- Program to find the sum of all digits of given number in Python
- Program to find minimum largest sum of k sublists in C++
- Python Program to find the sum of array
- Program to find sum of the 2 power sum of all subarray sums of a given array in Python
- Program to find sum of medians of all odd length sublists in C++
- Find the sum of array in Python Program
- Find two numbers whose sum and GCD are given in C++