Given a circular integer array nums of length n, return the maximum possible sum of a non-empty subarray of nums. A circular array means the end of the array connects to the beginning of the array. Formally, the next element of nums[i] is nums[(i + 1) % n] and the previous element of nums[i] is nums[(i - 1 + n) % n]. A subarray may only include each element of the fixed buffer nums at most once.
Example 1
Input: nums = [1,-2,3,-2]
Output: 3
Explanation: The best subarray is just [3]. Wrapping around to include 1 would also bring in both -2s, which lowers the sum.
Example 2
Input: nums = [5,-3,5]
Output: 10
Explanation: Wrapping around the end, the subarray [5,5] (last element, then first) sums to 10 and skips the -3 in the middle.
Example 3
Input: nums = [-3,-2,-3]
Output: -2
Explanation: Every number is negative, so the best choice is the single largest element, -2.
The best circular subarray is either a normal (non-wrapping) subarray — found by standard Kadane — or a wrapping one, which equals the total sum minus the smallest non-wrapping subarray. Take the larger, with one guard for the all-negative case.
Run checks all cases above. Submit evaluates all test cases.