3SUM


In computational complexity theory, the 3SUM problem asks if a given set of real numbers contains three elements that sum to zero. A generalized version, k-SUM, asks the same question on k numbers. 3SUM can be easily solved in time, and matching lower bounds are known in some specialized models of computation.
It was conjectured that any deterministic algorithm for the 3SUM requires 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 time.
Additionally, Grønlund and Pettie showed that the 4-linear decision tree complexity of 3SUM is.
These bounds were subsequently improved.
The current best known algorithm for 3SUM runs in time.
Kane, Lovett, and Moran showed that the 6-linear decision tree complexity of 3SUM is. The latter bound is tight.
It is still conjectured that 3SUM is unsolvable in expected time.
When the elements are integers in the range, 3SUM can be solved in time by representing the input set as a bit vector, computing the set of all pairwise sums as a discrete convolution using the Fast Fourier transform, and finally comparing this set to.

Quadratic algorithm

Suppose the input array is. In integer models of computing, 3SUM can be solved in time on average by inserting each number into a hash table, and then for each index and, checking whether the hash table contains the integer.
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 need to binary search for the pairs in the sorted list, achieving worst-case time, as follows.
sort;
for i = 0 to n - 2 do
a = S;
start = i + 1;
end = n - 1;
while do
b = S
c = S;
if 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 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
-25 -10 -7 -3 2 4 8 10
...
-25 -10 -7 -3 2 4 8 10
-25 -10 -7 -3 2 4 8 10
-25 -10 -7 -3 2 4 8 10
-25 -10 -7 -3 2 4 8 10
-25 -10 -7 -3 2 4 8 10
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 in the following way:
For e.g., if A= and if you are asked to find 3sum for C=4, then subtract all the elements of A by 4/3 and solve it in the usual 3sum way, i.e., + + = 0
Or you could simply modify the original algorithm to search the hash table for the integer.

3 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 :
By the way we transformed the arrays, it is guaranteed that.

Convolution sum

Instead of looking for arbitrary elements of the array such that:
the convolution 3sum problem looks for elements in specific locations:

Reduction from Conv3SUM to 3SUM

Given a solver for 3SUM, the Conv3SUM problem can be solved in the following way.
Correctness proof:
Given a solver for Conv3SUM, the 3SUM problem can be solved in the following way.
The reduction uses a hash function. As a first approximation, assume that we have a linear hash function, i.e. a function h such that:
Suppose that all elements are integers in the range: 0...N-1, and that the function h maps each element to an element in the smaller range of indices: 0...n-1. Create a new array T and send each element of S to its hash value in T, i.e., for every x in S:
Initially, suppose that the mappings are unique. Solve Conv3SUM on T. Now:
This idealized solution doesn't work, because any hash function might map several distinct elements of S to the same cell of T. The trick is to create an array T* by selecting a single random element from each cell of T, and run Conv3SUM on T*. If a solution is found, then it is a correct solution for 3SUM on S. If no solution is found, then create a different random T* and try again. Suppose there are at most R elements in each cell of T. Then the probability of finding a solution is the probability that the random selection will select the correct element from each cell, which is. By running Conv3SUM times, the solution will be found with a high probability.
Unfortunately, we do not have linear perfect hashing, so we have to use an almost linear hash function, i.e. a function h such that:
This requires to duplicate the elements of S when copying them into T, i.e., put every element both in and in. So each cell will have 2R elements, and we will have to run Conv3SUM times.

3SUM-hardness

A problem is called 3SUM-hard if solving it in subquadratic time implies a subquadratic-time algorithm for 3SUM. The concept of 3SUM-hardness was introduced by. They proved that a large class of problems in computational geometry are 3SUM-hard, including the following ones.
By now there are a multitude of other problems that fall into this category. An example is the decision version of X + Y sorting: given sets of numbers and of elements each, are there distinct for ?