
- 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 maximum sum of two non-overlapping sublists in Python
Suppose we have a list of numbers called nums and two values x and y, we have to find the maximum sum of two non-overlapping sublists in nums which have lengths x and y.
So, if the input is like nums = [3, 2, 10, -2, 7, 6] x = 3 y = 1, then the output will be 22, as the sublist with length 3 we select [3, 2, 10] and for the other we select [7].
To solve this, we will follow these steps −
- P := a list with single element 0
- for each x in A, do
- insert (last element of P + x) at the end of P
- Define a function solve() . This will take len1, len2
- Q := a list with element (P[i + len1] - P[i]) for each i in range 0 to size of P - len1
- prefix := a copy of Q
- for i in range 0 to size of prefix - 1, do
- prefix[i + 1] := maximum of prefix[i + 1] and prefix[i]
- ans := -infinity
- for i in range len1 to size of P - len2, do
- left := prefix[i - len1]
- right := P[i + len2] - P[i]
- ans := maximum of ans and (left + right)
- return ans
- From the main method do the following −
- return maximum of solve(len1, len2) , solve(len2, len1)
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, A, len1, len2): P = [0] for x in A: P.append(P[-1] + x) def solve(len1, len2): Q = [P[i + len1] - P[i] for i in range(len(P) - len1)] prefix = Q[:] for i in range(len(prefix) - 1): prefix[i + 1] = max(prefix[i + 1], prefix[i]) ans = float("-inf") for i in range(len1, len(P) - len2): left = prefix[i - len1] right = P[i + len2] - P[i] ans = max(ans, left + right) return ans return max(solve(len1, len2), solve(len2, len1)) ob = Solution() nums = [3, 2, 10, -2, 7, 6] x = 3 y = 1 print(ob.solve(nums, x, y))
Input
[3, 2, 10, -2, 7, 6], 3, 1
Output
22
- Related Articles
- Program to find sum of k non-overlapping sublists whose sum is maximum in C++
- Program to find largest sum of 3 non-overlapping sublists of same sum in Python
- Maximum Sum of Two Non-Overlapping Subarrays in C++
- Program to find maximum number of non-overlapping substrings in Python
- Program to find maximum number of non-overlapping subarrays with sum equals target using Python
- Maximum sum two non-overlapping subarrays of given size in C++
- Program to find two non-overlapping sub-arrays each with target sum using Python
- Maximum Sum of 3 Non-Overlapping Subarrays in C++
- Program to find sum of the sum of all contiguous sublists in Python
- Program to find the sum of the lengths of two nonoverlapping sublists whose sum is given in Python
- Program to find maximum sum of non-adjacent nodes of a tree in Python
- Program to find number of sets of k-non-overlapping line segments in Python
- Program to find number of sublists whose sum is given target in python
- Program to find number of sublists with sum k in a binary list in Python
- Maximum sum of lengths of non-overlapping subarrays with k as the max element in C++

Advertisements