Maximum Sum Circular Subarray
LeetCode 918 · Medium · solution, intuition & step-by-step walkthrough
answer = max(kadane(nums), total − min_subarray(nums)), unless everything is negative.
This is how I solved Maximum Sum Circular Subarray (LeetCode 918), a medium problem from the Kadane / Subarray DP pattern. Below: the intuition, the approach step by step, my accepted Python code, animated test cases and a line-by-line debugger.
Intuition
A best circular subarray either sits in the middle like a normal subarray, or it wraps around the ends. A wrapping subarray is the whole array minus some middle piece — so to maximise it, remove the minimum-sum middle subarray. Kadane gives both answers; the minimum comes from running Kadane on the negated array.
Approach
- 1
Run Kadane for the normal (non-wrapping) maximum.
- 2
If that maximum is negative, every element is negative — return it (the wrap formula would give an empty array).
- 3
Run Kadane on the negated array; its result is −(minimum subarray sum).
- 4
The wrapping candidate is total − minimum; return the larger candidate.
How I solved it
Kadane twicePythonO(n) time · O(n) space
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
max_sum = float("-inf")
cur_sum =0
for num in nums:
cur_sum+=num
max_sum = max(max_sum,cur_sum)
if cur_sum<0:
cur_sum=0
return max_sum
def maxSubarraySumCircular(self, nums: List[int]) -> int:
#max(max_sum_non_cricular, total_sum-minimum sum of the subarray)
#get the max of the non circular sub array
#if all negative then it's the maximum [-1,-2,-3,-5,-4] =>-15
#if not then find the minuimum sum of the circular sub array to keep the other part maximum
#so if we can get the min and substract from the total that's the maximum
min_sum_non_circular = self.maxSubArray(nums)
if min_sum_non_circular<0:
return min_sum_non_circular
minimum_sum_circular = self.maxSubArray([-num for num in nums])
return max(min_sum_non_circular,sum(nums)-(-minimum_sum_circular))Maximum Sum Circular Subarray — FAQ
What is the intuition behind Maximum Sum Circular Subarray?
A best circular subarray either sits in the middle like a normal subarray, or it wraps around the ends. A wrapping subarray is the whole array minus some middle piece — so to maximise it, remove the minimum-sum middle subarray. Kadane gives both answers; the minimum comes from running Kadane on the negated array.
How do you solve Maximum Sum Circular Subarray (LeetCode 918)?
1. Run Kadane for the normal (non-wrapping) maximum. 2. If that maximum is negative, every element is negative — return it (the wrap formula would give an empty array). 3. Run Kadane on the negated array; its result is −(minimum subarray sum). 4. The wrapping candidate is total − minimum; return the larger candidate.
What is the time and space complexity of Maximum Sum Circular Subarray?
Time: O(n). Space: O(n) for the negated copy (O(1) possible). (Accepted approach: Kadane twice.)
What are common mistakes in Maximum Sum Circular Subarray?
All-negative input: total − min would be 0 (empty subarray), which isn't allowed.