ArticleslgStudy

computer science

3SUM

3SUM is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand 3SUM rather than just read about it. In short: In computational complexity theory, the 3SUM problem asks if a given set of n real numbers contains three elements that sum to zero. A generalized version, k-SUM, asks the same question on k elements, rather than simply 3. 3SUM can be easily solved in O(n2) time, and matching Ω(n⌈k/2⌉) lower bounds are known in some specialized models of computation (Erickson 1999).

Key takeaways

  • 3SUM belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect 3SUM to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of 3SUM from memory before moving on to harder problems.

Reference excerpt

In computational complexity theory, the 3SUM problem asks if a given set of n real numbers contains three elements that sum to zero. A generalized version, k-SUM, asks the same question on k elements, rather than simply 3. 3SUM can be easily solved in O(n2) time, and matching Ω(n⌈k/2⌉) lower bounds are known in some specialized models of computation (Erickson 1999). It was conjectured that any deterministic algorithm for the 3SUM requires Ω(n2) time. In 2014, the original 3SUM conjecture was refuted by Allan Grønlund and Seth Pettie who gave a deterministic algorithm that solves 3SUM in O(n2/(log⁡n/log⁡log⁡n)2/3) time. Additionally, Grønlund and Pettie showed that the 4-linear decision tree complexity of 3SUM is O(n3/2log⁡n). These bounds were subsequently improved. In 2020 (improved on in 2026) the best known algorithm for 3SUM runs in O(n2(log⁡log⁡n)O(1)/log2n) time. Kane, Lovett, and Moran showed that the 6-linear decision tree complexity of 3SUM is O(nlog2n). The latter bound is tight (up to a logarithmic factor). It was conjectured that 3SUM is unsolvable in O(n2−Ω(1)) expected time, refuted in 2026. When the elements are integers in the range [−N,…,N], 3SUM can be solved in O(n+Nlog⁡N) time by representing the input set S as a bit vector, computing the set S+S of all pairwise sums as a discrete convolution using the fast Fourier transform, and finally comparing this set to S. In an October 2026 preprint, Josh Alman and Virginia Vassilevska Williams reported a deterministic algorithm solving 3SUM on polynomially bounded integers in O(n1.9992) time in the word RAM model, and an algorithm solving 3SUM on real numbers in O(n1.998) expected time in the real RAM model. The authors state that these results refute the integer and real-valued versions of the 3SUM conjecture (and refute other conjectures). The paper credits an internal research version of Claude with discovering the core algorithm (it also verified the paper’s main results using the Lean 4 proof assistant with the Mathlib library). Alman and Vassilevska Williams state that Anthropic shared a description of the algorithm with them, and compensated them for extending and writing up the results. The new result spurred more research. Days later on 12 October Ruhollah Majdoddin published a preprint with better bounds for 3SUM (and many other problems, such as related 3XOR) improving "both steps of their algorithm and obtain deterministic algorithms for 3SUM in n1.9945+o(1) time. .. also improve on the preprint's proof sketch for 3XOR and solve it deterministically in O(n1.99451) .. [the] paper was written with Anthropic’s Claude models, Claude Opus 5.5 and Claude Fable 5.1, through Claude Code, in October 2026. Under the author’s direction .. The Lean formalisation described in Section 22.2 was written in the same way. The author takes full responsibility for the content."

Quadratic algorithm Suppose the input array is S[0..n−1]. In integer (word RAM) models of computing, 3SUM can be solved in O(n2) time on average by inserting each number S[i] into a hash table, and then, for each index i and j, checking whether the hash table contains the integer −(S[i]+S[j]). It is also possible to solve the problem in the same time in a comparison-based model of computing or real RAM, for which hashing is not allowed. The algorithm below first sorts the input array and then tests all possible pairs in a careful order that avoids the slowdown of a binary search per pair, achieving worst-case O(n2) time, as follows.

sort(S); for i = 0 to n - 2 do a = S[i]; start = i + 1; end = n - 1; while (start < end) do b = S[start] c = S[end]; if (a + b + c == 0) then output a, b, c; // Continue search for all triplet combinations summing to zero. // We need to update both end and start together since the array values are distinct. start = start + 1; end = end - 1; else if (a + b + c > 0) then end = end - 1; else start = start + 1; end end

The following example shows this algorithm's execution on a small sorted array. Current values of a are shown in red, values of b and c are shown in magenta.

-25 -10 -7 -3 2 4 8 10 (a+b+c==-25) -25 -10 -7 -3 2 4 8 10 (a+b+c==-22) . . . -25 -10 -7 -3 2 4 8 10 (a+b+c==-7) -25 -10 -7 -3 2 4 8 10 (a+b+c==-7) -25 -10 -7 -3 2 4 8 10 (a+b+c==-3) -25 -10 -7 -3 2 4 8 10 (a+b+c==2) -25 -10 -7 -3 2 4 8 10 (a+b+c==0)

The correctness of the algorithm can be seen as follows. Suppose we have a solution a + b + c = 0. Since the pointers only move in one direction, we can run the algorithm until the leftmost pointer points to a. Run the algorithm until either one of the remaining pointers points to b or c, whichever occurs first. Then the algorithm will run until the last pointer points to the remaining term, giving the affirmative solution.

Variants

Non-zero sum Instead of looking for numbers whose sum is 0, it is possible to look for numbers whose sum is any constant C. The simplest way would be to modify the original algorithm to search the hash table for the integer ⁠(C−(S[i]+S[j]))⁠. Another method:

Subtract C/3 from all elements of the input array. In the modified array, find 3 elements whose sum is 0. For example, if A=[1,2,3,4] and if you are asked to find 3SUM for C=4, then subtract 4/3 from all the elements of A, and solve it in the usual 3sum way, i.e., ⁠(a−C/3)+(b−C/3)+(c−C/3)=0⁠.

Three different arrays Instead of searching for the 3 numbers in a single array, we can search for them in 3 different arrays. I.e., given three arrays X, Y and Z, find three numbers a∈X, b∈Y, c∈Z, such that ⁠a+b+c=0⁠. Call the 1-array variant 3SUM×1 and the 3-array variant 3SUM×3. Given a solver for 3SUM×1, the 3SUM×3 problem can be solved in the following way (assuming all elements are integers):

For every element in X, Y and Z, set: ⁠X[i]←X[i]∗10+1⁠, ⁠Y[i]←Y[i]∗10+2⁠, ⁠Z[i]←Z[i]∗10−3⁠. Let S be a concatenation of the arrays X, Y and Z. Use the 3SUM×1 oracle to find three elements ⁠a′∈S, b′∈S, c′∈S⁠ such that ⁠a′+b′+c′=0⁠. Return ⁠a←(a′−1)/10, b←(b′−2)/10, c←(c′+3)/10⁠. By the way we transformed the arrays, it is guaranteed that a∈X, b∈Y, c∈Z.

Convolution sum Instead of looking for arbitrary elements of the array such that:

S[k]=S[i]+S[j] the convolution 3sum problem (Conv3SUM) looks for elements in specific locations:

S[i+j]=S[i]+S[j]

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with 3SUM

Start with the simplest possible case. Write down what 3SUM claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to 3SUM before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about 3SUM ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of 3SUM

In research
3SUM appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses 3SUM in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
3SUM is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computational geometry, Polynomial-time problems, Unsolved problems in computer science, so understanding it makes those chapters shorter.
In everyday life
Look for 3SUM outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “3SUM” →

How to study 3SUM in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what 3SUM means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain 3SUM out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is 3SUM in simple terms?

In computational complexity theory, the 3SUM problem asks if a given set of n real numbers contains three elements that sum to zero. A generalized version, k-SUM, asks the same question on k elements, rather than simply 3. 3SUM can be easily solved in O(n2) time, and matching Ω(n⌈k/2⌉) lower bounds…

Why does 3SUM matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study 3SUM?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on 3SUM.

Tags

  • Computational geometry
  • Polynomial-time problems
  • Unsolved problems in computer science

Keep exploring