Skip to content

Maximum subarray problem

The problem of finding a contiguous interval of a numeric array whose elements have the maximum possible sum.

Version
v2 · 2026-11-14 · History
Domain-specific #
10624
Domain group
Applied Sciences & Engineering
Origin domain
Computer Science & Software Engineering
Subdomains
Algorithm Design, Dynamic Programming → Computer Science & Software Engineering
Aliases
Maximum sum subarray problem, Maximum segment sum problem

Core Idea

The maximum subarray problem asks for consecutive indices i through j whose array values have the greatest sum. Contiguity is constitutive: an internal negative value can remain when it connects larger positive contributions, while a prefix whose accumulated sum hurts every future extension can be discarded.

Kadane's linear-time recurrence tracks the best sum ending at the current position and the best seen globally. At each element it chooses between starting anew and extending the previous interval. A complete specification must say whether empty output is allowed, how ties are handled, and whether indices or only the sum are returned.

How would you explain it like I'm…

Best Candy Stretch

Imagine a row of boxes, and each has some coins inside or takes some coins away. You get to pick boxes that sit right next to each other, with no skipping. The puzzle is to find the stretch that leaves you with the most coins.

Biggest Unbroken Stretch

You have a list of numbers in a row, some positive and some negative. The maximum subarray problem asks which unbroken stretch of the list adds up to the biggest total. You can't skip over numbers in the middle. Sometimes it's worth keeping a negative number if it connects two big positive parts. But if the numbers so far add up to something that only hurts, it's better to drop them and start fresh.

Maximum Contiguous Sum

The maximum subarray problem: given an array of numbers, find consecutive positions i through j whose values have the largest sum. Contiguity is essential, which is what makes it different from just picking all the positive numbers. A negative value inside the stretch can be worth keeping if it links larger positive sections, while a beginning section whose total is harmful to anything after it can be thrown away. Kadane's algorithm solves it in one pass: at each element it decides whether to extend the current stretch or start a new one, and remembers the best total seen. A full problem statement also says whether an empty stretch is allowed, how ties are broken, and whether to return positions or only the sum.

 

The maximum subarray problem seeks indices i ≤ j maximizing the sum of the contiguous elements A[i..j]. Contiguity is constitutive: an internal negative element may be retained if it bridges larger positive contributions, whereas any prefix whose running sum would hurt every future extension can be discarded. Kadane's algorithm exploits this with a linear-time recurrence. It maintains the best sum of a subarray ending at the current position and the best sum seen globally. At each element, it chooses between starting a new interval at that element and extending the previous one. A complete specification must state whether the empty subarray is allowed (which matters for all-negative arrays), how ties are resolved, and whether the output is the index pair or just the sum.

Scope of Application

  • Algorithm design. Dynamic programming yields a linear scan.
  • Time-series analysis. High cumulative-score intervals are identified.
  • Image analysis. Higher-dimensional maximum-sum rectangles generalize the problem.
  • Teaching. The problem illustrates invariants, divide-and-conquer, and optimization transformations.

Clarity

Specify input type, contiguity, empty policy, output indices, tie-breaking, numeric precision, and dimensionality. 'Subsequence' should be avoided unless its contiguity convention is explicit. Inclusion test: A solution is a contiguous interval maximizing the sum under the stated empty/nonempty convention. Exclusion test: The maximum subset sum is excluded because it can skip internal negative entries. Nearest boundary: Maximum subsequence is a common terminological near miss; in algorithms, subsequence often permits noncontiguous selection whereas subarray does not. Exit condition: The identity exits when contiguity, one-dimensional order, additive scoring, or maximization is removed. Common misclassifications: It is not maximum subset sum. It is not necessarily the longest positive run. It is not identical across empty-allowed and nonempty conventions. It is not restricted to Kadane's algorithm, which is one solution method. Nearest named distinctions: Maximum subset sum: Can choose noncontiguous elements. Longest increasing subsequence: Optimizes length under order constraints rather than contiguous sum. Sliding-window maximum: Maximizes individual values or fixed-window aggregates. Maximum subsequence: Often permits gaps unless explicitly defined as contiguous.

Manages Complexity

An input of length n contains quadratically many intervals, yet the best-ending invariant reduces search to one pass. That compression depends on additive separability and linear order; multidimensional or constrained variants can be substantially harder.

Abstract Reasoning

  1. Fix empty/nonempty and tie conventions.
  2. Initialize the best-ending and global-best states consistently.
  3. For each value, compare starting a new interval with extending the prior best ending here.
  4. Update start metadata when starting anew.
  5. Update the global optimum and tie information.
  6. Return sum and requested indices.
  7. Validate against all-negative, all-positive, zero, empty, and tied cases.

Knowledge Transfer

The recurrence transfers to any ordered additive score where a harmful prefix can be abandoned. It stops at arbitrary subsets, nonadditive interactions, or constraints requiring more state. The cargo is optimal contiguous accumulation through a best-ending invariant.

Neighborhood in Abstraction Space

Maximum subarray problem sits in a crowded region of the domain-specific corpus (29th percentile for distinctiveness): several abstractions share nearly its structure, so a description that fits it tends to fit its neighbors too.

Family — Combinatorial Optimization & Game Problems (12 abstractions)

Nearest neighbors

Computed from structural-signature embeddings · 2026-11-14