Fastest way to find max sum range in int
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The fastest general solution for finding the contiguous integer range with the maximum sum is Kadane's algorithm. It runs in linear time, which is optimal because every element must be examined at least once. If you also need the start and end indices of the best range, you can extend the same algorithm with a few extra variables.
Why Brute Force Is Too Slow
A brute-force solution checks every possible range and sums it. That is easy to understand, but it is far too slow for large arrays.
Example brute-force shape:
This improved brute-force form is O(n^2). A fully naive version can be even worse. For real workloads, Kadane's algorithm is the right default.
Use Kadane's Algorithm for the Best Sum
Kadane's key idea is simple: for each position, either extend the current range or start a new range at the current element.
This prints 6, which comes from the range [4, -1, 2, 1].
The runtime is O(n) and memory usage is O(1).
Track the Actual Range Indices
Often the sum alone is not enough. You also need the range boundaries. That can be added without changing the linear complexity.
This version returns both the best sum and the inclusive range that produced it.
Handle All-Negative Arrays Correctly
A common bug is returning 0 when all elements are negative. That is only correct if the problem definition allows the empty subarray. Many formulations require a non-empty range, in which case the correct answer is the least negative element.
For example:
The right answer is:
- sum =
-2 - range = just the element at index
3
That is why robust implementations initialize from the first element instead of initializing everything to zero.
When the Problem Is Different
Kadane solves the maximum contiguous subarray problem. It does not solve:
- maximum sum of non-contiguous elements
- fixed-length window maximum sum
- maximum sum rectangle in a matrix
Those are different problems and need different algorithms. If the word "range" means contiguous segment in a one-dimensional integer array, Kadane is the standard answer.
Common Pitfalls
- Using a quadratic or cubic brute-force search when a linear algorithm exists.
- Initializing the best sum to zero and breaking all-negative input cases.
- Returning only the sum when the caller also needs the actual index range.
- Applying Kadane to a non-contiguous selection problem, which changes the problem entirely.
- Forgetting to validate empty-array input before accessing the first element.
Summary
- Kadane's algorithm is the fastest standard solution for maximum contiguous sum range in an integer array.
- It runs in
O(n)time with constant extra memory. - A few extra variables are enough to recover the start and end indices.
- Initialization from the first element is important for all-negative inputs.
- Make sure the problem really asks for a contiguous range before choosing this algorithm.

