Skip to main content
Back to problems
Leetcode
Medium
Arrays
Dynamic Programming
Google
Maximum Sum Circular Subarray

Find the maximum possible sum of a non-empty subarray in an array treated as circular.

Acceptance 0%
Problem Statement

Given a non-empty integer array, choose a non-empty contiguous subarray and return the maximum possible sum.

The array is circular, which means the element after the last element is the first element. So a subarray may either:

  • lie entirely within the array bounds, or
  • wrap around the end and continue from the beginning.

Your task is to compute the largest sum among all valid non-empty circular subarrays.

Input Format

  • An integer array nums of length n.
  • Each nums[i] is an integer.

Output Format

  • Return a single integer: the maximum circular subarray sum.

Constraints

  • 1 <= n
  • The subarray must be non-empty.
  • Values may be positive, negative, or zero.
  • The answer should fit in a standard 32-bit signed integer in typical interview settings.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [1,-2,3,-2]

Output

3

Explanation

The best subarray is [3] with sum 3. Wrapping does not help here.

Example 2

Input

nums = [5,-3,5]

Output

10

Explanation

Take the circular subarray [5] at the end plus [5] at the beginning, giving 10.

Show 1 more example

Example 3

Input

nums = [-3,-2,-3]

Output

-2

Explanation

All numbers are negative, so the answer is the largest single element, which is -2.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.