2

To be specific, the problem is formalized as follows.

Given a set of integers $\{a_1,\ldots,a_n\}$, determine whether there exist non-negative integers $x_1,\ldots,x_n$ such that $a_1x_1+\cdots+a_nx_n=0$ and at least one of $x_1,\ldots,x_n$ is positive.

Note this is not a duplicate of this problem. In that problem, the target sum is given as input, while in our problem, the target sum is fixed to be $0$.

I tried to follow the idea of the answer to that question to come up with a new reduction, but since a basic idea of that answer is to use big elements as well as big target to prevent an element from being chosen multiple times, while in our problem the target sum is $0$, I failed.

Related question: Is the following Subset Sum variant NP-complete?

xskxzr
  • 7,613
  • 5
  • 24
  • 47

2 Answers2

4

The problem can be solved in polynomial time.

If any of the $a_i$ are zero, then the answer is "yes" (you can set the corresponding $x_i$ to 1, and all other $x_j$ to 0). So let's assume all of the $a_i$ are non-zero.

If all of the $a_i$ are strictly positive, then the answer is "no" (if $x_i$ is the one that's set to something non-zero, then $x_i>0$ and $a_i>0$, so $a_1 x_1 + \dots + a_n x_n \ge a_i x_i > 0$). So we can assume at least one of the $a_i$ is strictly negative.

If all of the $a_i$ are strictly negative, then the answer is "no" (if $x_i$ is the one that's set to something non-zero, then $x_i>0$ and $a_i<0$, so $a_1 x_1 + \dots + a_n x_n \le a_i x_i < 0$). So we can assume at least one of the $a_i$ is strictly negative.

If at least one of the $a_i$ is strictly positive and at least one is strictly negative, say $a_i>0$ and $a_j<0$, then the answer is "yes" (you can take $x_i=-a_j$, $x_j=a_i$, and set all other $x$'s to zero; then $a_1 x_1 + \dots + a_n x_n = a_i x_i + a_j x_j = -a_i a_j + a_i a_j = 0$).

These cases cover all the possibilities: at least one of the above cases must hold. So, we obtain a polynomial-time algorithm for the original problem.

Credit: I got the key idea from Discrete lizard.

D.W.
  • 167,959
  • 22
  • 232
  • 500
2

This problem lies in P.

To see this, formulate this problem it as an integer linear program (ILP):

For $x\in \mathbb{Z}^n$, maximize $\sum_{i=1}^n x_i$ under the constraint $\sum_{i=1}^n a_ix_i = 0$ and $x_i\geq 0$ for all $i\in \{1,\ldots,n\}$.

Let's have a look at the relaxed version of this ILP:

For $x\in \mathbb{R}^n$, maximize $\sum_{i=1}^n x_i$ under the constraint $\sum_{i=1}^n a_ix_i = 0$ and $x_i\geq 0$ for all $i\in \{1,\ldots,n\}$.

Observe that if $x$ is a solution of the relaxed LP, then $x\in \mathbb{Q}^n$, as $\sum_{i=1}^n a_ix_i = 0$ and all $a_i$'s are integral. Furthermore, if $x\in \mathbb{Q}^n$ is a solution, then $x'$ such that $x'_i = c\cdot x_i$ is also a solution for any $c\in \mathbb{Z}$. Therefore, if the relaxed LP has a (non-zero) solution, there also exists a non-zero solution to the LP in $\mathbb{Z}^n$ (take $c$ as the least common multiple of all denominators).

So, if there is a non-zero solution to the relaxed LP, our ILP has a non-zero solution and therefore there exists a solution to your input. Since we can solve LP's over $\mathbb{R}^n$ in polynomial time, we have a polynomial algorithm for this problem.

Note that, as @D.W. mentioned, solving LP's in polynomial time can be a tricky business. Fortunately, (existence of a non-zero solution for) this LP can be solved by hand, as shown in this answer. In particular, there exists such a solution if and only if at least one of the $a_i$'s is positive and at least one is negative.

Discrete lizard
  • 8,392
  • 3
  • 25
  • 53