If $x,y \in [1,n]$, how to write $x \neq y$ in integer-linear constraint?

Possible Answer:

$x−y\geq 1−(1−t)\times n$ and $y−x \geq 1−t\times n$ where $t \in \{0,1\}$

  • 2
    $\begingroup$ Do you refer to integer linear programming, or linear programming? $\endgroup$
    – D.W.
    Commented Apr 22 at 20:08
  • $\begingroup$ It is not a linear constraint, say $-1 \neq 1$ and $1\neq -1$ but $-1+1 = 1-1$. $=$ and $\geq$ are linear. $\endgroup$
    – Ethan
    Commented Apr 23 at 20:44

1 Answer 1


With linear programming: you can't.

With integer linear programming, here is one solution: introduce zero-or-one variable $t$, and the inequalities

$$x \le y - 1 + (1-t)n, \qquad x \ge y+1 - tn.$$

Intuition: if $t$ is 1, then we must have $x<y$; if $t$ is 0, then we must have $x>y$.

I have assumed that $x,y$ are integer variables and $n$ is a known constant.

My thanks to @Subhankar Ghosal for an improvement to my initial answer, which I have incorporated here.

  • $\begingroup$ Can we have the following? $x - y \geq 1 - (1-t)n$ and $y - x \geq 1 - tn$ $\endgroup$ Commented Apr 25 at 12:59
  • $\begingroup$ @SubhankarGhosal, oh good point, that is a better solution. I have revised my answer accordingly. $\endgroup$
    – D.W.
    Commented Apr 25 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.