Program to find the sum of largest K sublist in Python

When we have a list of numbers and need to find the maximum sum of a contiguous sublist from the list concatenated k times, we can use Kadane's algorithm with optimization for large k values.

The key insight is that we only need to process at most 2 complete iterations of the original list to find the optimal solution, then add the contribution from remaining iterations if beneficial.

Algorithm Steps

The algorithm works as follows:

  • Initialize sum (s), answer (ans), and lowest prefix sum (lo) to 0
  • Iterate through the list at most min(k, 2) times to find the maximum subarray sum
  • For each element, update the running sum and track the minimum prefix sum seen so far
  • Update the answer with the maximum difference between current sum and minimum prefix sum
  • Add contribution from remaining iterations if the total list sum is positive

Example

Let's implement the solution to find the maximum subarray sum from a list concatenated k times ?

class Solution:
    def solve(self, nums, k):
        s = ans = lo = 0
        
        # Process at most 2 complete iterations
        for _ in range(min(k, 2)):
            for x in nums:
                s += x
                lo = min(lo, s)
                ans = max(ans, s - lo)
        
        # Add contribution from remaining iterations if beneficial
        return ans + max(0, sum(nums)) * max(0, (k - 2))

# Test the solution
solution = Solution()
nums = [2, 4, 5, -4]
k = 1
result = solution.solve(nums, k)
print(f"Maximum subarray sum: {result}")
Maximum subarray sum: 11

How It Works

For the input nums = [2, 4, 5, -4] and k = 1:

  • We process the array once since k = 1
  • The maximum contiguous subarray is [2, 4, 5] with sum = 11
  • Since k = 1, no additional iterations are needed

Testing with Different Values

Let's test with different k values to see how the algorithm handles multiple concatenations ?

solution = Solution()

# Test case 1: k = 1
nums1 = [2, 4, 5, -4]
k1 = 1
print(f"nums = {nums1}, k = {k1}")
print(f"Result: {solution.solve(nums1, k1)}")

# Test case 2: k = 3 with positive sum
nums2 = [1, -2, 3]
k2 = 3
print(f"\nnums = {nums2}, k = {k2}")
print(f"Result: {solution.solve(nums2, k2)}")

# Test case 3: k = 2 with negative sum
nums3 = [-1, -2, -3]
k3 = 2
print(f"\nnums = {nums3}, k = {k3}")
print(f"Result: {solution.solve(nums3, k3)}")
nums = [2, 4, 5, -4], k = 1
Result: 11

nums = [1, -2, 3], k = 3
Result: 6

nums = [-1, -2, -3], k = 2
Result: -1

Key Points

  • Efficiency: The algorithm runs in O(n) time by processing at most 2 iterations
  • Kadane's Algorithm: Uses the principle of tracking maximum subarray sum
  • Optimization: For k > 2, adds the total sum multiplied by (k-2) if positive
  • Edge Cases: Handles negative sums and single iterations correctly

Conclusion

This solution efficiently finds the maximum subarray sum from a list concatenated k times using a modified Kadane's algorithm. The key optimization is recognizing that we only need to examine at most 2 complete iterations to find the optimal solution.

Updated on: 2026-03-25T12:31:32+05:30

319 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements