Maximum circular subarray
Standard · Write code · about 15 min
nums is circular: after the last value comes the first again. A subarray may wrap past the end, but it may use each value at most once.
Return the largest sum of a non-empty subarray.
Example 1
Input:
[[5,-3,5]]
Output:
10
Wrap around: 5 + 5 = 10, skipping −3.
Example 2
Input:
[[1,-2,3,-2]]
Output:
3
3 alone; no wrap helps.
Constraints
- 1 ≤ nums.length ≤ 10,000
- −10,000 ≤ nums[i] ≤ 10,000
Learn the idea first: Kadane's algorithm and maximum subarray, a visual lesson.
More Kadane's algorithm and maximum subarray problems
- Follow Kadane's two numbers Warm-up
- Best time to buy and sell once Standard
- Maximum product subarray Challenge