Maximum Sum Circular Subarray

Medium#918best subarray

You get an array of integers nums arranged in a ring: after the last element comes the first again. Find the subarray with the largest sum and return that sum. A subarray is a block of one or more elements in a row, with no gaps; here it may go past the end and on from the start, but takes each element at most once. Our example: [5, -2, -6, 3, -1, 4] — the answer is 11, from the subarray [3, -1, 4, 5] that goes past the end.

input
nums: an array of integers closed into a circle: nums[0] comes after the last number.
returns
The largest sum of a subarray of the circle: one or more numbers in a row, which may run past the end on to the start; each number is taken at most once.
constraints
  • 1 ≤ nums.length ≤ 1000
  • −3·10⁴ ≤ nums[i] ≤ 3·10⁴
our 13 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {number[]} nums
 * @return {number}
 */
function maxSubarraySumCircular(nums) {

}
Ctrl ↵
format
reset

saved in this browser

13 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more dynamic programming (dp) problems

Pattern “Dynamic programming (DP)” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.