kb/data/en.wikipedia.org/wiki/3SUM-0.md

8.4 KiB
Raw Blame History

title chunk source category tags date_saved instance
3SUM 1/3 https://en.wikipedia.org/wiki/3SUM reference science, encyclopedia 2026-05-05T11:01:54.553179+00:00 kb-cron

In computational complexity theory, the 3SUM problem asks if a given set of

    n
  

{\displaystyle n}

real numbers contains three elements that sum to zero. A generalized version,

    k
  

{\displaystyle k}

-SUM, asks the same question on

    k
  

{\displaystyle k}

elements, rather than simply 3. 3SUM can be easily solved in

    O
    (
    
      n
      
        2
      
    
    )
  

{\displaystyle O(n^{2})}

time, and matching

    Ω
    (
    
      n
      
        ⌈
        k
        
          /
        
        2
        ⌉
      
    
    )
  

{\displaystyle \Omega (n^{\lceil k/2\rceil })}

lower bounds are known in some specialized models of computation (Erickson 1999). It was conjectured that any deterministic algorithm for the 3SUM requires

    Ω
    (
    
      n
      
        2
      
    
    )
  

{\displaystyle \Omega (n^{2})}

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
    (
    
      n
      
        2
      
    
    
      /
    
    (
    
      log
      
      n
    
    
      /
    
    
      log
      
      log
      
      n
    
    
      )
      
        2
        
          /
        
        3
      
    
    )
  

{\displaystyle O(n^{2}/({\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
    (
    
      n
      
        3
        
          /
        
        2
      
    
    
      
        log
        
        n
      
    
    )
  

{\displaystyle O(n^{3/2}{\sqrt {\log n}})}

. These bounds were subsequently improved. The current best known algorithm for 3SUM runs in

    O
    (
    
      n
      
        2
      
    
    (
    log
    
    log
    
    n
    
      )
      
        O
        (
        1
        )
      
    
    
      /
    
    
      
        log
        
          2
        
      
      
      n
    
    )
  

{\displaystyle O(n^{2}(\log \log n)^{O(1)}/{\log ^{2}n})}

time. Kane, Lovett, and Moran showed that the 6-linear decision tree complexity of 3SUM is

    O
    (
    n
    
      
        log
        
          2
        
      
      
      n
    
    )
  

{\displaystyle O(n{\log ^{2}n})}

. The latter bound is tight (up to a logarithmic factor).
It is still conjectured that 3SUM is unsolvable in

    O
    (
    
      n
      
        2
        
        Ω
        (
        1
        )
      
    
    )
  

{\displaystyle O(n^{2-\Omega (1)})}

expected time. When the elements are integers in the range

    [
    
    N
    ,
    …
    ,
    N
    ]
  

{\displaystyle [-N,\dots ,N]}

, 3SUM can be solved in

    O
    (
    n
    +
    N
    log
    
    N
    )
  

{\displaystyle O(n+N\log N)}

time by representing the input set

    S
  

{\displaystyle S}

as a bit vector, computing the set

    S
    +
    S
  

{\displaystyle S+S}

of all pairwise sums as a discrete convolution using the fast Fourier transform, and finally comparing this set to

    S
  

{\displaystyle S}

.

== Quadratic algorithm == Suppose the input array is

    S
    [
    0..
    n
    
    1
    ]
  

{\displaystyle S[0..n-1]}

. In integer (word RAM) models of computing, 3SUM can be solved in

    O
    (
    
      n
      
        2
      
    
    )
  

{\displaystyle O(n^{2})}

time on average by inserting each number

    S
    [
    i
    ]
  

{\displaystyle S[i]}

into a hash table, and then, for each index

    i
  

{\displaystyle i}

and

    j
  

{\displaystyle j}

, checking whether the hash table contains the integer

    
    (
    S
    [
    i
    ]
    +
    S
    [
    j
    ]
    )
  

{\displaystyle -(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
    (
    
      n
      
        2
      
    
    )
  

{\displaystyle O(n^{2})}

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
    ]
    )
    )
  

{\displaystyle (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
  

{\displaystyle (a-C/3)+(b-C/3)+(c-C/3)=0}

.