In computational complexity theory, the 3SUM problem asks if a given set of <math>n</math> real numbers contains three elements that sum to zero. A generalized version, <math>k</math>-SUM, asks the same question on <math>k</math> elements, rather than simply 3. 3SUM can be easily solved in <math>O(n^2)</math> time, and matching <math>\Omega(n^{\lceil k/2 \rceil})</math> lower bounds are known in some specialized models of computation .
It was conjectured that any deterministic algorithm for the 3SUM requires <math> \Omega(n^2) </math> 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 <math>O(n^2 / ({\log n} / {\log \log n})^{2/3})</math> time.
Additionally, Grønlund and Pettie showed that the 4-linear decision tree complexity of 3SUM is <math> O(n^{3/2}\sqrt{\log n}) </math>.
These bounds were subsequently improved.
The current best known algorithm for 3SUM runs in <math>O(n^2 (\log \log n)^{O(1)} / {\log^2 n}) </math> time.
Kane, Lovett, and Moran showed that the 6-linear decision tree complexity of 3SUM is <math>O(n{\log^2 n})</math>. The latter bound is tight (up to a logarithmic factor).
It is still conjectured that 3SUM is unsolvable in <math>O(n^{2-\Omega(1)})</math> expected time.
Quadratic algorithm
Suppose the input array is <math>S[0..n-1]</math>. In integer (word RAM) models of computing, 3SUM can be solved in <math>O(n^2)</math> time on average by inserting each number <math>S[i]</math> into a hash table, and then, for each index <math>i</math> and <math>j</math>, checking whether the hash table contains the integer <math>-(S[i]+S[j])</math>.
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 <math>O(n^2)</math> 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.
<span style="color:red">-25</span> <span style="color:magenta">-10</span> -7 -3 2 4 8 <span style="color:magenta">10</span> (a+b+c==-25)
<span style="color:red">-25</span> -10 <span style="color:magenta">-7</span> -3 2 4 8 <span style="color:magenta">10</span> (a+b+c==-22)
. . .
<span style="color:red">-25</span> -10 -7 -3 2 4 <span style="color:magenta">8</span> <span style="color:magenta">10</span> (a+b+c==-7)
-25 <span style="color:red">-10</span> <span style="color:magenta">-7</span> -3 2 4 8 <span style="color:magenta">10</span> (a+b+c==-7)
-25 <span style="color:red">-10</span> -7 <span style="color:magenta">-3</span> 2 4 8 <span style="color:magenta">10</span> (a+b+c==-3)
-25 <span style="color:red">-10</span> -7 -3 <span style="color:magenta">2</span> 4 8 <span style="color:magenta">10</span> (a+b+c==2)
-25 <span style="color:red">-10</span> -7 -3 <span style="color:magenta">2</span> 4 <span style="color:magenta">8</span> 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 .
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., .
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 , such that . 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: , , .
- Let S be a concatenation of the arrays X, Y and Z.
- Use the 3SUM×1 oracle to find three elements such that .
- Return .
By the way we transformed the arrays, it is guaranteed that .
Convolution sum
Instead of looking for arbitrary elements of the array such that:
:<math>S[k]=S[i]+S[j]</math>
the convolution 3sum problem (Conv3SUM) looks for elements in specific locations:
:<math>S[i+j]=S[i]+S[j]</math>
Reduction from Conv3SUM to 3SUM
Given a solver for 3SUM, the Conv3SUM problem can be solved in the following way.
See also
- Subset sum problem
Notes
References
- .
- .
- .
- .
- .
- .
