3SUM & the k-SUM Problem
From quadratic 3SUM with two pointers to the 3SUM conjecture.
"Are there three numbers in this list that sum to zero?" takes one line to ask and time to answer with two pointers. Three decades of work have shaved only logarithmic factors off that bound, and the conjecture that nothing truly faster exists now carries the weight of lower bounds for dozens of problems, from collinear points to dynamic data structures.
What you’ll learn
- Define 3SUM and k-SUM with their equivalent variants, and solve k-SUM in time (with an extra for even ) by meet in the middle.
- State Erickson's lower bound with its model, and explain why it rules out neither faster algorithms nor shallower trees with richer queries.
- State the 3SUM conjecture precisely and prove that deciding whether three of points are collinear is 3SUM-hard.
- Summarize the logarithmic speedups and decision-tree results up to 2025, and the open problems they leave.
3SUM and its relatives
The variants are equivalent up to linear-time reductions. A target becomes 0 by replacing each integer with . The form is the three-list form with negated. Three lists become one by shifting: replace by , by and by for a large . Of the ten ways to pick three offsets from , only is zero, so with only triples with one element from each list can cancel.
Two quadratic algorithms are standard. Sort and two pointers (the Optimized lesson) runs in worst-case time using only additions and comparisons, so it works on reals. Hashing fixes and asks, for each , whether was seen between them: expected time on the word RAM.
def three_sum_hash(a):
"""Is there i < j < k with a[i] + a[j] + a[k] == 0? O(n^2) expected time."""
n = len(a)
for i in range(n):
seen = set() # values a[i+1..j-1]
for j in range(i + 1, n):
if -a[i] - a[j] in seen:
return True
seen.add(a[j])
return False
print(three_sum_hash([5, -7, 3, 9, 2])) # True: 5 + (-7) + 2 == 0All inputs are integers in . What is the fastest correct way to decide 3SUM?
- Sort and two pointers: is the best known for every input range
- Hashing, which runs in expected time
- Multiply the polynomial by itself twice with the FFT and read the coefficient of :
- Sort, then binary search for :
Show the answer
C. Multiply the polynomial by itself twice with the FFT and read the coefficient of :
The coefficient of in counts ordered triples (repeats included) with sum 0; subtracting the triples that reuse a position, read off and , leaves the genuine ones. With values in this costs , so hard instances need : the integer form of the conjecture is stated for ranges such as . See The Fast Fourier Transform.
k-SUM: meet in the middle, and a matching lower bound
Even . Let hold all sums with one element from each of , and the sums from . A solution is exactly a pair , with . Sort both, then run the pair-sum walk on two sorted lists:
Odd . Build and sort and from the first lists once. For each of the elements , walk and looking for , in linear time each:
For (), and are sorted copies of two lists, and the bound is the familiar . Replacing the sorted lists by a hash set of removes the logarithm for even , in expectation.
def four_sum_exists(a, b, c, d):
"""Is there w + x + y + z == 0 with w in a, x in b, y in c, z in d?"""
left = sorted(w + x for w in a for x in b) # n^2 sums, sorted
right = sorted(y + z for y in c for z in d)
i, j = 0, len(right) - 1
while i < len(left) and j >= 0: # pair sum on two sorted lists
s = left[i] + right[j]
if s == 0:
return True
if s < 0:
i += 1 # left[i] is too small even with the largest right sum left
else:
j -= 1 # right[j] is too big even with the smallest left sum left
return False4-SUM in time and space; the same walk as pair sum, on lists of half-sums.
The proof is an adversary argument. Very roughly, the adversary builds an input with critical -tuples: each could be turned into a solution by an infinitesimal perturbation (Erickson works with formal infinitesimals) that changes the sign of no other query. A -linear query notices such a perturbation only if its hyperplane is that tuple's own equation, so a correct tree must ask about every critical tuple separately. For odd the bound matches meet in the middle exactly.
The theorem is about a model, and the model matters. Grønlund and Pettie (2014) showed that 3SUM has 4-linear decision trees of depth . Kane, Lovett and Moran (2018) went further: for every constant , k-SUM has linear decision trees with queries, each comparing the sums of two -subsets, so every query is -sparse with coefficients in . Allowing a few more variables per query collapses the bound from to nearly .
Kane, Lovett and Moran decide 3SUM with comparison queries. Why does this not give an -time algorithm?
- Each comparison query costs time to evaluate
- A decision tree counts only queries: deciding which query to ask next, and building the tree, is free in the model but costs time in a real algorithm
- The bound is amortized over many inputs
- It only works for integer inputs
Show the answer
B. A decision tree counts only queries: deciding which query to ask next, and building the tree, is free in the model but costs time in a real algorithm
Decision-tree depth measures information, not computation. The tree may be different for every and arbitrarily hard to construct, and the work of choosing each next query is not charged. Turning shallow trees into fast algorithms is exactly where the logarithmic-speedup papers spend their effort.
The 3SUM conjecture and 3SUM-hardness
Gajentaan and Overmars (1995) introduced 3SUM-hardness to explain why so many problems in computational geometry were stuck at quadratic time. Their original question was whether is optimal; Grønlund and Pettie (2014) refuted that strong form with algorithms running in divided by a polylogarithm. The modern conjecture asks for a polynomial improvement and is still open. The cleanest reduction turns sums into slopes.
For distinct and , the slope between and is
Three distinct points are collinear exactly when the slopes from to the other two agree:
Factor out , which is nonzero because the numbers are distinct:
Repeated values are handled separately: a solution that uses some value twice, , is found in time by sorting. So a subquadratic collinearity algorithm yields a subquadratic 3SUM algorithm.
Think first The numbers sum to 0. Check that , and lie on one line. Why would a parabola, , not work?
The slopes are , and : one line of slope 7, and as predicted. On a parabola the slope between and is , so equal slopes from force : no three distinct points of a parabola are ever collinear, and the reduction would say nothing.
The same paper shows that many other problems are 3SUM-hard, among them whether three of lines pass through one point (the dual of collinearity), whether a union of triangles has a hole, and planar motion-planning questions. Pătraşcu (2010) proved, by a randomized reduction, that the convolution version, find with , is as hard as 3SUM, and from it derived polynomial lower bounds for dynamic data structures and for listing the triangles of a graph. Today the 3SUM conjecture is one of the three pillars of fine-grained complexity, beside the APSP conjecture and SETH.
Shaving logarithms: the state of the art
| Year | Result | Model |
|---|---|---|
| 1995/1999 | Erickson: queries for k-SUM | -linear decision trees |
| 2005 | Baran, Demaine & Pătraşcu: expected time | integers, word RAM |
| 2014 | Grønlund & Pettie: depth ; time deterministic, randomized | reals: 4-linear trees; real RAM |
| 2018 | Chan: time | reals, real RAM |
| 2018 | Kane, Lovett & Moran: queries for k-SUM, any constant | -sparse comparison queries |
Much of this rests on Fredman's trick (1976): exactly when . A comparison between two sums becomes a comparison between two differences, one from each side. Split the sorted input into blocks of elements; the differences inside each block can be sorted once and reused across many scans. That idea is at the core of Grønlund and Pettie's shallow trees and, combined with word-level or table-lookup tricks, of the faster algorithms.
How much do these logarithms buy? At , , before constant factors that are typically large. The practical algorithm remains sort and two pointers; the logarithmic results matter because they show that the comparisons a naive argument would charge are not the true bottleneck.
Key takeaways
- 3SUM takes worst-case time by sorting and two pointers, or expected by hashing; small integer ranges fall to the FFT, so hard instances have values of size around or more.
- Meet in the middle solves k-SUM in time (times for even ), and Erickson proved this optimal for -linear decision trees, but 4-linear and -sparse comparison trees need far fewer queries.
- The 3SUM conjecture says no algorithm exists; only its original "exactly " form has fallen, to algorithms.
- 3SUM-hardness transfers the conjecture: mapping makes "three collinear points" equivalent to "".
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
With meet in the middle, how much time does 5-SUM on five lists of numbers take?
Show the answer
C.
: sort the sums of lists 1–2 and of lists 3–4, then for each of the elements of list 5 run a linear walk over the two sorted lists of sums: .
Which statement about 3SUM is true as of 2025?
- An -time algorithm is known
- It is proven to need time on the word RAM
- Its decision-tree complexity is for linear queries of every kind
- It can be solved in divided by a polylogarithm, but no algorithm is known
Show the answer
D. It can be solved in divided by a polylogarithm, but no algorithm is known
Logarithmic speedups exist for integers (2005) and reals (2014, 2018). The conjecture that no polynomial speedup exists remains open, and it underlies many conditional lower bounds.
The reduction from 3SUM to collinearity maps to . Why not to ?
- Squares lose the sign of , but the reduction would otherwise work
- No three distinct points of a parabola are collinear, so collinearity would carry no information
- Squares are too small to represent in -bit words
- It would also work; cubes are just conventional
Show the answer
B. No three distinct points of a parabola are collinear, so collinearity would carry no information
The slope between and is ; equal slopes from force . With cubes the slope is , and equal slopes factor into .
Which of these problems is 3SUM-hard, so that an algorithm for it would refute the 3SUM conjecture?
- Sorting numbers
- Deciding whether two of numbers sum to 0
- Deciding whether three of lines in the plane pass through a common point
- Finding the closest pair among points in the plane
Show the answer
C. Deciding whether three of lines in the plane pass through a common point
Concurrent lines are the projective dual of collinear points, one of Gajentaan and Overmars's original 3SUM-hard problems. The other three take time, which refutes nothing.
Further reading
- A. Gajentaan & M. H. Overmars, "On a class of problems in computational geometry," Computational Geometry: Theory and Applications (1995).
- J. Erickson, "Lower bounds for linear satisfiability problems," Chicago Journal of Theoretical Computer Science (1999); extended abstract at SODA 1995.
- I. Baran, E. D. Demaine & M. Pătraşcu, "Subquadratic algorithms for 3SUM," WADS 2005; Algorithmica (2008).
- A. Grønlund & S. Pettie, "Threesomes, degenerates, and love triangles," FOCS 2014; Journal of the ACM (2018).
- D. M. Kane, S. Lovett & S. Moran, "Near-optimal linear decision trees for k-SUM and related problems," STOC 2018; Journal of the ACM (2019).