538 lines
7.0 KiB
Markdown
538 lines
7.0 KiB
Markdown
---
|
||
title: "3SUM"
|
||
chunk: 2/3
|
||
source: "https://en.wikipedia.org/wiki/3SUM"
|
||
category: "reference"
|
||
tags: "science, encyclopedia"
|
||
date_saved: "2026-05-05T11:01:54.553179+00:00"
|
||
instance: "kb-cron"
|
||
---
|
||
|
||
=== 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
|
||
|
||
|
||
{\displaystyle 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
|
||
|
||
|
||
{\displaystyle X[i]\gets X[i]*10+1}
|
||
|
||
,
|
||
|
||
|
||
|
||
Y
|
||
[
|
||
i
|
||
]
|
||
←
|
||
Y
|
||
[
|
||
i
|
||
]
|
||
∗
|
||
10
|
||
+
|
||
2
|
||
|
||
|
||
{\displaystyle Y[i]\gets Y[i]*10+2}
|
||
|
||
,
|
||
|
||
|
||
|
||
Z
|
||
[
|
||
i
|
||
]
|
||
←
|
||
Z
|
||
[
|
||
i
|
||
]
|
||
∗
|
||
10
|
||
−
|
||
3
|
||
|
||
|
||
{\displaystyle Z[i]\gets 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
|
||
|
||
|
||
{\displaystyle a'\in S,\ b'\in S,\ c'\in S}
|
||
|
||
such that
|
||
|
||
|
||
|
||
|
||
a
|
||
′
|
||
|
||
+
|
||
|
||
b
|
||
′
|
||
|
||
+
|
||
|
||
c
|
||
′
|
||
|
||
=
|
||
0
|
||
|
||
|
||
{\displaystyle a'+b'+c'=0}
|
||
|
||
.
|
||
Return
|
||
|
||
|
||
|
||
a
|
||
←
|
||
(
|
||
|
||
a
|
||
′
|
||
|
||
−
|
||
1
|
||
)
|
||
|
||
/
|
||
|
||
10
|
||
,
|
||
|
||
b
|
||
←
|
||
(
|
||
|
||
b
|
||
′
|
||
|
||
−
|
||
2
|
||
)
|
||
|
||
/
|
||
|
||
10
|
||
,
|
||
|
||
c
|
||
←
|
||
(
|
||
|
||
c
|
||
′
|
||
|
||
+
|
||
3
|
||
)
|
||
|
||
/
|
||
|
||
10
|
||
|
||
|
||
{\displaystyle a\gets (a'-1)/10,\ b\gets (b'-2)/10,\ c\gets (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
|
||
]
|
||
|
||
|
||
{\displaystyle S[k]=S[i]+S[j]}
|
||
|
||
|
||
the convolution 3sum problem (Conv3SUM) looks for elements in specific locations:
|
||
|
||
|
||
|
||
|
||
S
|
||
[
|
||
i
|
||
+
|
||
j
|
||
]
|
||
=
|
||
S
|
||
[
|
||
i
|
||
]
|
||
+
|
||
S
|
||
[
|
||
j
|
||
]
|
||
|
||
|
||
{\displaystyle S[i+j]=S[i]+S[j]}
|
||
|
||
|
||
==== Reduction from Conv3SUM to 3SUM ====
|
||
Given a solver for 3SUM, the Conv3SUM problem can be solved in the following way.
|
||
|
||
Define a new array T, such that for every index i:
|
||
|
||
|
||
|
||
T
|
||
[
|
||
i
|
||
]
|
||
=
|
||
2
|
||
n
|
||
S
|
||
[
|
||
i
|
||
]
|
||
+
|
||
i
|
||
|
||
|
||
{\displaystyle T[i]=2nS[i]+i}
|
||
|
||
(where n is the number of elements in the array, and the indices run from 0 to n-1).
|
||
Solve 3SUM on the array T.
|
||
Correctness proof:
|
||
|
||
If in the original array there is a triple with
|
||
|
||
|
||
|
||
S
|
||
[
|
||
i
|
||
+
|
||
j
|
||
]
|
||
=
|
||
S
|
||
[
|
||
i
|
||
]
|
||
+
|
||
S
|
||
[
|
||
j
|
||
]
|
||
|
||
|
||
{\displaystyle S[i+j]=S[i]+S[j]}
|
||
|
||
, then
|
||
|
||
|
||
|
||
T
|
||
[
|
||
i
|
||
+
|
||
j
|
||
]
|
||
=
|
||
2
|
||
n
|
||
S
|
||
[
|
||
i
|
||
+
|
||
j
|
||
]
|
||
+
|
||
i
|
||
+
|
||
j
|
||
=
|
||
(
|
||
2
|
||
n
|
||
S
|
||
[
|
||
i
|
||
]
|
||
+
|
||
i
|
||
)
|
||
+
|
||
(
|
||
2
|
||
n
|
||
S
|
||
[
|
||
j
|
||
]
|
||
+
|
||
j
|
||
)
|
||
=
|
||
T
|
||
[
|
||
i
|
||
]
|
||
+
|
||
T
|
||
[
|
||
j
|
||
]
|
||
|
||
|
||
{\displaystyle T[i+j]=2nS[i+j]+i+j=(2nS[i]+i)+(2nS[j]+j)=T[i]+T[j]}
|
||
|
||
, so this solution will be found by 3SUM on T.
|
||
Conversely, if in the new array there is a triple with
|
||
|
||
|
||
|
||
T
|
||
[
|
||
k
|
||
]
|
||
=
|
||
T
|
||
[
|
||
i
|
||
]
|
||
+
|
||
T
|
||
[
|
||
j
|
||
]
|
||
|
||
|
||
{\displaystyle T[k]=T[i]+T[j]}
|
||
|
||
, then
|
||
|
||
|
||
|
||
2
|
||
n
|
||
S
|
||
[
|
||
k
|
||
]
|
||
+
|
||
k
|
||
=
|
||
2
|
||
n
|
||
(
|
||
S
|
||
[
|
||
i
|
||
]
|
||
+
|
||
S
|
||
[
|
||
j
|
||
]
|
||
)
|
||
+
|
||
(
|
||
i
|
||
+
|
||
j
|
||
)
|
||
|
||
|
||
{\displaystyle 2nS[k]+k=2n(S[i]+S[j])+(i+j)}
|
||
|
||
. Because
|
||
|
||
|
||
|
||
i
|
||
+
|
||
j
|
||
<
|
||
2
|
||
n
|
||
|
||
|
||
{\displaystyle i+j<2n}
|
||
|
||
, necessarily
|
||
|
||
|
||
|
||
S
|
||
[
|
||
k
|
||
]
|
||
=
|
||
S
|
||
[
|
||
i
|
||
]
|
||
+
|
||
S
|
||
[
|
||
j
|
||
]
|
||
|
||
|
||
{\displaystyle S[k]=S[i]+S[j]}
|
||
|
||
and
|
||
|
||
|
||
|
||
k
|
||
=
|
||
i
|
||
+
|
||
j
|
||
|
||
|
||
{\displaystyle k=i+j}
|
||
|
||
, so this is a valid solution for Conv3SUM on S.
|
||
|
||
==== Reduction from 3SUM to Conv3SUM ====
|
||
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:
|
||
|
||
|
||
|
||
|
||
h
|
||
(
|
||
x
|
||
+
|
||
y
|
||
)
|
||
=
|
||
h
|
||
(
|
||
x
|
||
)
|
||
+
|
||
h
|
||
(
|
||
y
|
||
)
|
||
|
||
|
||
{\displaystyle h(x+y)=h(x)+h(y)}
|
||
|
||
|
||
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(
|
||
|
||
|
||
|
||
∀
|
||
x
|
||
∈
|
||
S
|
||
|
||
|
||
{\displaystyle \forall x\in S}
|
||
|
||
):
|
||
|
||
|
||
|
||
|
||
T
|
||
[
|
||
h
|
||
(
|
||
x
|
||
)
|
||
]
|
||
=
|
||
x
|
||
|
||
|
||
{\displaystyle T[h(x)]=x}
|
||
|
||
|
||
Initially, suppose that the mappings are unique (i.e. each cell in T accepts only a single element from S). Solve Conv3SUM on T. Now: |