Find the maximum possible sum of a non-empty subarray in an array treated as circular.
Given a non-empty integer array, choose a non-empty contiguous subarray and return the maximum possible sum.
The array is circular, which means the element after the last element is the first element. So a subarray may either:
Your task is to compute the largest sum among all valid non-empty circular subarrays.
nums of length n.nums[i] is an integer.1 <= nExample 1
Input
nums = [1,-2,3,-2]
Output
3
Explanation
The best subarray is [3] with sum 3. Wrapping does not help here.
Example 2
Input
nums = [5,-3,5]
Output
10
Explanation
Take the circular subarray [5] at the end plus [5] at the beginning, giving 10.
Example 3
Input
nums = [-3,-2,-3]
Output
-2
Explanation
All numbers are negative, so the answer is the largest single element, which is -2.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.