Maximum Sum Circular Subarray

Medium

Description

Given a circular integer array nums, find the maximum possible sum of a non-empty subarray of nums. A circular array wraps around, so the next element of nums[n-1] is nums[0].

Examples

Input:nums = [1,-2,3,-2]
Output:3
Explanation:

Subarray [3] has maximum sum 3.

Input:nums = [5,-3,5]
Output:10
Explanation:

Circular subarray [5,5] wrapping around has sum 10.

Input:nums = [-3,-2,-3]
Output:-2
Explanation:

Works with negative numbers.

Constraints

  • n == nums.length
  • 1 ≤ n ≤ 3 × 10⁴
  • -3 × 10⁴ ≤ nums[i] ≤ 3 × 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!