<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>

class Solution {
public:

int Leetcode_53_maxSubArray(vector<int>& nums) {
	int cur = 0;
	int res = 0;
	int res2 = nums[0];

	bool foundpos = false;

	for (int i = 0; i < nums.size(); i++)
	{
		if (nums[i] >= 0)
		{
			// Nicht alle Zahlen sind negativ
			foundpos = true;
		}

		// speichere die größte negative Zahl / kleinster Betrag
		res2 = max(res2, nums[i]);

		if (nums[i] < 0)
		{
			// lohnt es sich, die negative Zahl mitzunehmen?
			if (cur + nums[i] > 0)
			{
				cur += nums[i];
			}
			else
			{
				cur = 0;
			}
		}
		else
		{
			cur += nums[i];
		}

		// speichere die größte Zwischensumme cur
		res = max(res, cur);
	}

	// wenn alle Zahlen negativ sind, gebe Zahl mit niedrigstem Betrag zurück.
	if (!foundpos) return res2;

	return res;
}

    int maxSubarraySumCircular(vector<int>& nums) {
  

	auto res0 = Leetcode_53_maxSubArray(nums);
	if (res0 < 0) return res0;

	// nach links
	vector<int> tl(nums.size(), 0);
	tl[nums.size() - 1] = nums[nums.size() - 1];
	for (int i = nums.size() - 2; i >= 0; i--)
	{
		tl[i] = tl[i + 1] + nums[i];
	}
	int curmax = 0;
	for (int i = nums.size() - 1; i >= 0; i--)
	{
		curmax = max(curmax, tl[i]);
		tl[i] = curmax;
	}

	// nach rechts
	vector<int> tr(nums.size(), 0);
	tr[0] = nums[0];
	for (int i = 1; i < nums.size(); i++)
	{
		tr[i] = tr[i - 1] + nums[i];
	}
	curmax = 0;
	for (int i = 0; i < nums.size(); i++)
	{
		curmax = max(curmax, tr[i]);
		tr[i] = curmax;
	}

	int res = 0;

	for (int i = 0; i < nums.size()-1; i++)
	{
		res = max(res,tr[i] + tl[i+1]);
	}

	res = max(res, tr[tr.size()-1]);

	res = max(res, res0);

	return res;
    }
};
45300