# Suppose we have two variables $x,y \in [1,n]$. How can we write $x \neq y$ in integer-linear constraint?

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

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

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

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; 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.

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