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

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,679
  • 6
  • 15