All problems
Maximum Sum Circular Subarray
Problem
Return the maximum sum of a non-empty subarray of nums, where the subarray may wrap around the end of the array.
Compute the best ordinary subarray with Kadane's algorithm and the best circular one as the total minus the minimum subarray. If every value is negative, return the ordinary maximum.
Hint
Removing the minimum middle subarray from the total leaves the best wrap-around sum.
Test cases
case 1
in: [[1,-2,3,-2]]
expected: 3
case 2
in: [[5,-3,5]]
expected: 10
case 3
in: [[3,-1,2,-1]]
expected: 4
case 4
in: [[-3,-2,-5]]
expected: -2
case 5
in: [[1]]
expected: 1
Starter codePython
def max_circular_subarray(nums):
# Your code here
pass5 test cases · run your code against them in the editor