- 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

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 −

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))

[3, 2, 10, -2, 7, 6], 3, 1

22

- Related Questions & Answers
- 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
- Maximum sum two non-overlapping subarrays of given size in C++
- Program to find maximum number of non-overlapping subarrays with sum equals target using Python
- 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
- Minimum Size of Two Non-Overlapping Intervals in C++
- Program to find number of sublists whose sum is given target in python
- Program to find maximum length of non-sharing words in Python

Advertisements