#918

Maximum Sum Circular Subarray

Medium Kadane max & min

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

The array is circular (the end wraps to the start). Return the largest possible sum of a non-empty subarray that uses each element at most once.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: best subarray when the array wraps around.

Why this works: either the best piece doesn't wrap (normal Kadane), or it wraps, which means it's the total minus the worst middle piece.

Example: [5, -3, 5]

no wrap (Kadane): 5, 2, 7 → best 7wrap: total 7 - min piece (-3) = 10 → [5, 5]answer 10

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Forgetting the all-negative case, which returns 0 (an empty subarray) instead of the largest element.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(1).
Topic: Kadane, Dutch Flag & Moore's Voting
🎬 Episodes 11–13: video coming soon