Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find largest sum of 3 non-overlapping sublists of same sum in Python
Finding the largest sum of three non-overlapping sublists of the same size is a dynamic programming problem. We need to efficiently calculate sublist sums and track the maximum possible combinations.
So, if the input is like nums = [2, 2, 2, -6, 4, 4, 4, -8, 3, 3, 3] k = 3, then the output will be 27, as we can select the sublists [2, 2, 2], [4, 4, 4], and [3, 3, 3], total sum is 27.
Algorithm Steps
To solve this, we will follow these steps ?
- Create prefix sum array P to calculate sublist sums efficiently
- Calculate all possible sublist sums of size k in array Q
- Build prefix array to track maximum sum from left side
- Build suffix array to track maximum sum from right side
- For each middle sublist, find maximum sum combining left, middle, and right parts
Implementation
Let us see the following implementation to get better understanding ?
class Solution:
def solve(self, nums, k):
# Create prefix sum array
prefix_sum = [0]
for x in nums:
prefix_sum.append(prefix_sum[-1] + x)
# Calculate all sublist sums of size k
sublist_sums = [prefix_sum[i + k] - prefix_sum[i] for i in range(len(prefix_sum) - k)]
# Create arrays to track maximum sums from left and right
left_max = sublist_sums[:]
right_max = sublist_sums[:]
# Fill left_max array (maximum sum from left up to each position)
for i in range(len(sublist_sums) - 1):
left_max[i + 1] = max(left_max[i + 1], left_max[i])
# Fill right_max array (maximum sum from right up to each position)
for i in range(len(sublist_sums) - 1):
right_max[~(i + 1)] = max(right_max[~(i + 1)], right_max[~i])
# Find maximum sum of three non-overlapping sublists
return max(sublist_sums[i] + left_max[i - k] + right_max[i + k]
for i in range(k, len(sublist_sums) - k))
# Test the solution
solution = Solution()
nums = [2, 2, 2, -6, 4, 4, 4, -8, 3, 3, 3]
k = 3
result = solution.solve(nums, k)
print(f"Input: {nums}, k = {k}")
print(f"Output: {result}")
Input: [2, 2, 2, -6, 4, 4, 4, -8, 3, 3, 3], k = 3 Output: 27
How It Works
The algorithm works in several phases:
- Prefix Sum: Creates cumulative sum array for O(1) sublist sum calculation
- Sublist Sums: Calculates sum of every possible sublist of size k
- Left Maximum: Tracks the maximum sublist sum from the beginning up to each position
- Right Maximum: Tracks the maximum sublist sum from each position to the end
- Final Calculation: For each valid middle position, combines the best left, middle, and right sublists
Example Breakdown
For the input [2, 2, 2, -6, 4, 4, 4, -8, 3, 3, 3] with k=3:
nums = [2, 2, 2, -6, 4, 4, 4, -8, 3, 3, 3]
k = 3
# Sublist sums of size 3:
# [2,2,2] = 6, [2,2,-6] = -2, [2,-6,4] = 0, [-6,4,4] = 2
# [4,4,4] = 12, [4,4,-8] = 0, [4,-8,3] = -1, [-8,3,3] = -2, [3,3,3] = 9
print("Possible sublists of size 3:")
for i in range(len(nums) - k + 1):
sublist = nums[i:i+k]
print(f"Index {i}: {sublist} = {sum(sublist)}")
Possible sublists of size 3: Index 0: [2, 2, 2] = 6 Index 1: [2, 2, -6] = -2 Index 2: [2, -6, 4] = 0 Index 3: [-6, 4, 4] = 2 Index 4: [4, 4, 4] = 12 Index 5: [4, 4, -8] = 0 Index 6: [4, -8, 3] = -1 Index 7: [-8, 3, 3] = -2 Index 8: [3, 3, 3] = 9
The optimal solution selects sublists at indices 0, 4, and 8 with sums 6 + 12 + 9 = 27.
Time Complexity
The time complexity is O(n) where n is the length of the input array. The space complexity is also O(n) for storing the prefix sums and auxiliary arrays.
Conclusion
This dynamic programming approach efficiently finds three non-overlapping sublists with maximum total sum. The key insight is using prefix and suffix arrays to track optimal choices for each position.
