---
# System prepended metadata

title: Leetcode 918. Maximum Sum Circular Subarray
tags: [Leetcode]

---

[link](https://leetcode.com/problems/maximum-sum-circular-subarray/)

---

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. Formally, for a subarray nums[i], nums[i + 1], ..., nums[j], there does not exist i <= k1, k2 <= j with k1 % n == k2 % n.

#### Example 1:

```
Input: nums = [1,-2,3,-2]
Output: 3
Explanation: Subarray [3] has maximum sum 3.
```

#### Example 2:

```
Input: nums = [5,-3,5]
Output: 10
Explanation: Subarray [5,5] has maximum sum 5 + 5 = 10.
```

#### Example 3:

```
Input: nums = [-3,-2,-3]
Output: -2
Explanation: Subarray [-2] has maximum sum -2.
```

#### Constraints:

- n == nums.length
- 1 <= n <= 3 * 104
- -3 * 104 <= nums[i] <= 3 * 104

---

The variables globMax and globMin are initialized to the first element of nums. They represent the global maximum and minimum sums found so far. The variables curMax and curMin are initialized to 0. They represent the current maximum and minimum sums of the subarray. The variable total is initialized to 0. It keeps track of the sum of all elements in nums.

The code iterates through the list of integers using a loop variable n. In each iteration, the current element n is added to curMax and curMin to update the maximum and minimum sums of the subarray. If curMax + n is greater than n, it means extending the current subarray would increase the sum. Otherwise, it's better to start a new subarray from n. Therefore, curMax is updated to the maximum between curMax + n and n. Similarly, curMin is updated to the minimum between curMin + n and n. The total value is updated by adding n to it.

The globMax value is updated by taking the maximum between the current globMax and curMax. This keeps track of the global maximum sum. The globMin value is updated by taking the minimum between the current globMin and curMin. This keeps track of the global minimum sum. After completing the loop, the globMax and globMin values contain the global maximum and minimum sums of the subarray.

The maximum sum of the subarray is determined by comparing globMax with total - globMin. If globMax is positive, it means the maximum sum lies within the circular subarray. Otherwise, it lies outside the circular subarray. The maximum of these two values is returned as the result. If globMax is not positive, it means all elements in nums are negative or zero, and the maximum sum is the maximum negative or zero value in nums. In this case, globMax is returned as the result.

#### Solution 1
```python=
class Solution:
    def maxSubarraySumCircular(self, nums: List[int]) -> int:
        globMax, globMin = nums[0], nums[0]
        curMax, curMin = 0, 0
        total = 0
        for n in nums:
            curMax = max(curMax + n, n)
            curMin = min(curMin + n, n)
            total += n
            globMax = max(globMax, curMax)
            globMin = min(globMin, curMin)

        return max(globMax, total - globMin) if globMax > 0 else globMax
        
```
O(T): O(n)
O(S): O(1)
