Maximum Sum Circular Subarray
A wrapping block is the whole circle minus one middle block, so its sum is total − (sum of the middle). To make the wrap as large as possible, make the middle block as small as possible. Run Kadane twice in one pass: once for the largest block and once for the smallest. The answer is the bigger of largest and total − smallest.
A wrapping block is total minus one non-empty middle block, so the best wrap is total minus the smallest middle block. The one exception is when every number is negative.
Problem
You are given an integer array nums that is circular: the element after the last one is the first one. A block of numbers can wrap around, going from the end of the array back to the start.
Return the largest sum of a non-empty block. A block may not use the same number twice.
An integer array nums of length n, read as a circle.
A single integer: the largest sum of any non-empty block on the circle.
The best block is [3] by itself. Wrapping does not help: [3, -2, 1] gives 2 and [1] gives 1.
The block wraps around. The last 5 and the first 5 sit next to each other, so [5, 5] gives 10. Keeping the -3 in between would only give 7.
Every number is negative, so the best block is the least negative single number, -2.
- • n == nums.length
- • 1 ≤ n ≤ 3 × 10^4
- • −3 × 10^4 ≤ nums[i] ≤ 3 × 10^4
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
For a normal array, Kadane finds the best block. What new kind of block appears when the array is a circle?
- 2.
A block that wraps uses the end and the start of the array. Which part of the array does it leave out?
- 3.
If a wrapping block skips a middle part, how is its sum related to the total sum of the array?
- 4.
What happens to my answer if every single number is negative?
- 5.
How many blocks can a circle of n numbers have, and can I check them all fast enough?
Brute force approach
Better approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Walk every block on the circle, using % n to wrap; keep the largest sum | O(N^2) | O(1) |
| Better | Prefix sums give any plain block; a wrap pairs the best prefix with the best suffix | O(N) | O(N) |
| Optimal | Run Kadane for the largest and the smallest block sum; answer = max(largest, total − smallest) | O(N) | O(1) |
Why does this belong to this pattern?
- “circular array”The array is a circle, so a block can wrap from the end back to the start.
- “non-empty block”The answer is a contiguous block on the circle. A wrapping block is the complement of one middle block.
- “largest sum”Maximising the wrap means minimising the middle, so the same Kadane runs for the smallest sum too.
This is Kadane applied to the complement. A plain maximum-subarray question stops at the array's ends, but a circular block can wrap, and a wrapping block is the total minus one middle block. So the same Kadane that finds the largest sum also finds the smallest middle block, and the answer uses both. The all-negative guard is the one extra rule that only the circle needs.
Don't make this mistake
On [-3, -2, -3] the smallest middle block is the whole array, which sums to -8. So total - minK = -8 - (-8) = 0. That is the empty block, and the code returns 0 instead of -2. Every number is negative, so the best answer is the least negative number, -2.
On the dry run [5, -4, 6, -3, -2, 5] the total is 7 and the largest plain block is 7. So total - maxK = 0, which is not a real block. The real wrap is 7 - (-5) = 12, which removes the smallest middle block [-3, -2].
The brute force must wrap. On [5, -4, 6, -3, -2, 5], start 4 with length 3 reads index 6, which does not exist. The modulo sends it back to index 0, the 5 at the front of the array.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like DP — Kadane / Subarray translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.