if i need to divide the array into two subsets with the minimum diference between the sum of the two sets what i can do i know the dp(idx,sum) solution but i need faster one
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | jiangly | 3631 |
| 4 | Kevin114514 | 3574 |
| 5 | maroonrk | 3521 |
| 6 | strapple | 3515 |
| 7 | Radewoosh | 3461 |
| 8 | tourist | 3428 |
| 9 | turmax | 3378 |
| 10 | Um_nik | 3376 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 162 |
| 2 | adamant | 148 |
| 3 | Um_nik | 146 |
| 4 | Dominater069 | 143 |
| 5 | errorgorn | 140 |
| 6 | cry | 138 |
| 7 | Proof_by_QED | 136 |
| 8 | YuukiS | 135 |
| 9 | chromate00 | 134 |
| 10 | soullless | 133 |
| Name |
|---|



Consider the DP:
Then think of the path of the DP that leads to the optimal solution. Since
sumis minimized in the last step, maybe it's also small in every intermediate step? This is not always true. But what if we shuffle the array?Then we can think of each $$$a_{i}$$$ as an independent random variable (which is not really true, but close enough for CP purposes), and
summight follow something like a random walk, which is normally distributed according to CLT. Due to linearity of variance of independent variables, we expect that $$$|\texttt{sum}|$$$ is always $$$O(\sqrt{N}\cdot\max\{ a_{i}\})$$$ with high probability.So this means we can write above DP solution like this:
f(0, sum) = abs(sum) f(i, sum) = if abs(sum) > LIMIT then INF else min(f(i-1, sum+a[i]). f(i-1, sum-a[i]))(Where $$$\texttt{LIMIT} \in O(\sqrt{N}\cdot\max\{ a_{i}\})$$$ -- multiply by 3 or 4) And we still get the correct answer with high probability, assuming that
ahas been shuffled beforehand. With this method we a probabilistic solution with complexity $$$O(N^{1.5}\cdot\max\{ a_{i}\})$$$ instead of the classic $$$O(N^{2}\cdot\max\{ a_{i}\})$$$Thanks