<p>Given a <strong>circular integer array</strong> <code>nums</code> of length <code>n</code>, return <em>the maximum possible sum of a non-empty <strong>subarray</strong> of </em><code>nums</code>.</p>

<p>A <strong>circular array</strong> means the end of the array connects to the beginning of the array. Formally, the next element of <code>nums[i]</code> is <code>nums[(i + 1) % n]</code> and the previous element of <code>nums[i]</code> is <code>nums[(i - 1 + n) % n]</code>.</p>

<p>A <strong>subarray</strong> may only include each element of the fixed buffer <code>nums</code> at most once. Formally, for a subarray <code>nums[i], nums[i + 1], ..., nums[j]</code>, there does not exist <code>i &lt;= k1</code>, <code>k2 &lt;= j</code> with <code>k1 % n == k2 % n</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,-2,3,-2]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Subarray [3] has maximum sum 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [5,-3,5]
<strong>Output:</strong> 10
<strong>Explanation:</strong> Subarray [5,5] has maximum sum 5 + 5 = 10.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [-3,-2,-3]
<strong>Output:</strong> -2
<strong>Explanation:</strong> Subarray [-2] has maximum sum -2.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == nums.length</code></li>
	<li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li>
	<li><code>-3 * 10<sup>4</sup> &lt;= nums[i] &lt;= 3 * 10<sup>4</sup></code></li>
</ul>

int crossing_max(vector<int>& nums,int low,int mid,int high)
{
    int rm=INT_MIN;
    int lm=INT_MIN;
    int s=0;
    for(int i=mid;i>=low;i--)
    {
        s=s+nums[i];
        lm=max(s,lm);
    }
    s=0;
    for(int i=mid+1;i<=high;i++)
    {
        s=s+nums[i];
        rm=max(s,rm);
    }
    return rm+lm;
}
int divide_max(vector<int>& nums,int low,int high)
{
     if(low==high) return nums[low];
    else
    {
        int mid=(low+high)/2;
        int left=divide_max(nums,low,mid);
        int right=divide_max(nums,mid+1,high);
        int cross=crossing_max(nums,low,mid,high);
        return max({left,right,cross});
    }
   
}
int crossing_min(vector<int>& nums,int low,int mid,int high)
{
    int rm=INT_MAX;
    int lm=INT_MAX;
    int s=0;
    for(int i=mid;i>=low;i--)
    {
        s=s+nums[i];
        lm=min(s,lm);
    }
    s=0;
    for(int i=mid+1;i<=high;i++)
    {
        s=s+nums[i];
        rm=min(s,rm);
    }
    return rm+lm;
}
int divide_min(vector<int>& nums,int low,int high)
{
    if(low==high) return nums[low];
    else
    {
        int mid=(low+high)/2;
        int left=divide_min(nums,low,mid);
        int right=divide_min(nums,mid+1,high);
        int cross=crossing_min(nums,low,mid,high);
        return min({left,right,cross});
    }
   
}
class Solution {
public:
    int maxSubarraySumCircular(vector<int>& nums) {
        int tot_sum=0;
        int n=nums.size();
        for(int i=0;i<n;i++)
        {
            tot_sum=tot_sum+nums[i];
        }
        int max_subarr=divide_max(nums,0,n-1);
        int min_subarr=divide_min(nums,0,n-1);
        if(tot_sum-min_subarr==0)
        return max_subarr;
        return max(max_subarr,tot_sum-min_subarr);
    }
};
43700