0

Following up on these two posts Generalised 3SUM (k-SUM) problem? https://people.csail.mit.edu/virgi/6.s078/lecture9.pdf

The claim is that k-sum in the general case can be solved in $O(n^{k/2}log(n))$

However, I don't follow this claim. The quote reads

For even : Compute a sorted list of all sums of /2 input elements. Check whether contains both some number and its negation −. The algorithm runs in (/2log) time.

Compute a list of all sums of k/2 input elements. $O(n^{k/2})$

Sort this list: $O(n^{k/2}log(n^{k/2})=O(k/2*n^{k/2}log(n))$

Sandwich with two pointers to find s and -s. We have a valid answer iff the indices of elements that make up s and -s are nonoverlapping. However, because we have to check each instance of -s in order to validate whether the indices are non-overlapping, we end up having a computation that is $O(n^{k/2})$. This means that this step is $O(n^{k/2} * n^{k/2})$.

Am I misunderstanding an optimization?

Jason
  • 1
  • Depending on the definition they are using the indices don't have to be distinct. – plshelp Aug 31 '22 at 15:22
  • Yes but say we desire distinct indices, which is the case for the general statement of the k-sum problem – Jason Aug 31 '22 at 16:05
  • 1
    If you are fine with a randomized algorithm then you can randomly partition the input collection into two sets $A$ and $B$ and consider the variant in which you need to choose $k/2$ elements from each set. If there is a solution to the original instance, then there is a probability of at least $\frac{\binom{k}{k/2}}{2^k}$ that this solution is evenly split between $A$ and $B$ (I'm assuming that $k$ is even for simplicity) which, using Stirling's approximations and dropping constants, is roughly $\frac{1}{\sqrt{k}}$. Repeat $\sqrt{k}\log\frac{1}{p}$ times for a failure probability of about $p$. – Steven Sep 02 '22 at 00:08

1 Answers1

0

In case we desire that all indices are distinct one can use balanced BSTs such as an AVL-Tree to check if indices overlap. For each of the $O(n^{k/2})$ sums you also store the set of indices (using a balanced BST as Data structure). Now creating all these sets takes $O(n^{k/2}k/2\log(k/2))$, because we need to insert $k/2$ elements in every set. Now when check if there exists $-n$ for any $n$ we need to check if their sets overlap. This again takes $O(k/2\log(k/2))$ or for every number $O(n^{k/2}k/2\log(k/2))$. Thus for constant $k$ we still maintain the runtime $O(n^{k/2}\log(n))$

plshelp
  • 1,619
  • 5
  • 14
  • Using a BST means that checking the intersection of two sums will take $O(k/2 log(k/2))$. However, if you store indices in sets, you can accomplish this in $O(k/2)$. The core problem is still that because it takes $O(n^{k/2}k/2)$ to check every number, and there are $O(n^{k/2})$ numbers, your run time becomes $O(n^k*k/2)$ – Jason Sep 01 '22 at 15:39
  • @JasonKang I don't understand - finding a $-n$ for given $n$ only takes log-time since we have sorted the numbers? So for every number we do a binary search to find a matching negative number and then check if indice sets overlap. Where does your runtime come from? – plshelp Sep 04 '22 at 02:59
  • There can be multiple numbers of the same sum. Given the numbers [1234] the array of k/2 (in this case 2) sums includes 5 twice (once from 1,4 and 2,3). Although you can bound the number of duplicates that occur from k/2 sums, it will likely be on the order of O(k). Checking each duplicate is expensive in k – Jason Sep 05 '22 at 03:39
  • To add to that, just considering ${0,1,2,...,m}$ there are $O(m/2)$ ways to get $m$ as a 2-sum. So the duplicates are probably polynomial in $m$ for $k$-sums. I don't know how to resolve this. – plshelp Sep 06 '22 at 05:41